ts-option-type
v3.1.1
Published
Rust inspired Option type - a union type wrapper of value T - Some<T> or None
Downloads
3
Maintainers
Readme
Option
Type for Typescript
Option
is a TypeScript utility that represents an optional value, which can either be Some
wrapping a value or None
.
Use it to avoid the billion dollar mistake!
Table of Contents
Installation
Depending on your package manager, use one of the following commands:
npm
npm install ts-option-type
pnpm
pnpm add ts-option-type
yarn
yarn add ts-option-type
Tests
npm run test
Examples
1. Basic Usage
import { Some, None } from 'ts-option-type'
const maybeValue: Option<number> = Some(5)
const result = maybeValue.match(
(value) => `Value is: ${value}`,
() => "No value found"
)
console.log(result) // Outputs: "Value is: 5"
2. Handling Defaults
const noValue: Option<string> = None
console.log(noValue.getValueOrDefault("Default String")) // Outputs: "Default String"
3. Chaining Methods
const initialValue: Option<number> = Some(4)
const transformedValue = initialValue
.map(value => value * 2)
.filter(value => value > 5)
console.log(transformedValue.isSome) // Outputs: true
4. Computation Fallbacks
const emptyValue: Option<string> = None
const result = emptyValue.getValueOrCompute(() => "Computed Value")
console.log(result) // Outputs: "Computed Value"
5. Combining Multiple Options (set operations)
const first: Option<number> = Some(1)
const second: Option<number> = Some(2)
const combined = first.and(second)
console.log(combined === second) // Outputs: true
console.log(None.and(first) === None) // Outputs: true
console.log(None.or(first) === first) // Outputs: true
6. Creation Patterns
Create and manipulate Option
types using different patterns to fit various scenarios:
import { fromValue, fromValueConditional, fromNullable, fromUndefinable, createOptionFactory } from 'ts-option-type'
// 1. Convert any value to Option, treating null or undefined as None
const maybeValue = fromValue("Hello") // Some<string>
// 2. Convert a value to Option based on a custom condition
const isEmptyStr = (value: string) => value.length === 0
const maybeStr = fromValueConditional("", isEmptyStr) // None
// 3. Convert a possibly nullable object to Option
const nullObj: {} | null | undefined = null
const maybeObj = fromNullable(nullObj) // None - fromNullable(undefined) would have been Some!
// 4. Convert a possibly undefinable object to Option
const undefinedObj: {} | null | undefined = undefined
const maybeDef = fromUndefinable(undefinedObj) // None - fromUndefinable(null) would have been Some!
// 5. Create a custom Option factory using a predicate - when predicate is true, we get None
const isNegative = (value: number) => value < 0
const positiveNumberOptionFactory = createOptionFactory(isNegative)
const maybeNum = positiveNumberOptionFactory(5) // Some<number>
Testing
To run tests:
npm run test
License
This project is licensed under the MIT License - see the LICENSE file for details.