Wrapped value type.
Applies appropriate function and returns result from the function.
some(1).caseOf(x => x + 1, () => 0) // 2
none.caseOf(x => x + 1, () => 0) // 0
Processing function for Some.
Processing function for None.
some(1).chainToOpt(x => x === 1 ? null : x + 1) // None
some(2).chainToOpt(x => x === 1 ? null : x + 1) // Some(3)
Compares inner value with given value using ===. Always false for None.
some(1).contains(1) // true
some(0).contains(1) // false
none.contains(undefined) // false
Is a value of this instance and given other instance the same?
Default comparator function is === (referential equality).
none.equals(none) // true
some(1).equals(none) // false
some(1).equals(some(1)) // true
some(1).equals(some(2)) // false
some({a: 1}).equals(some({a: 1})) // false (different objects)
some(1).equals(some(2), (x, y) => true) // true (custom comparator function)
const jsonCmp = <T>(a: T, b: T): boolean => JSON.stringify(a) === JSON.stringify(b);
some({a: 1}).equals(some({a: 1}), jsonCmp) // true (comparing values converted to JSON)
Applies p to inner value and passes result. Always false for None.
some(0).exists(x => x > 0) // false
some(1).exists(x => x > 0) // true
none.exists(x => x > 0) // false
Predicate.
some(1).flatBimap(x => some(x+1), () => some(0)) // Some(2)
none.flatBimap(x => some(x+1), () => some(0)) // Some(0)
some(5).flatBimap(x => none, () => some(0)) // None
Applies p to inner value and passes result. Always true for None.
some(0).forAll(x => x > 0) // false
some(1).forAll(x => x > 0) // true
none.forAll(x => x > 0) // true
Predicate.
Is this instance of None?
Is this instance of Some?
Narrows type inside Opt using given type guard.
some('1' as string | number).narrow(isString) // Some('1'): Opt<string>
some(1 as string | number).narrow(isString) // None: Opt<string>
some(1).optOrCrash('fail') // Some(1)
none.optOrCrash('fail') // throws
Returns value when Some, throws error with msg otherwise.
Error message.
some(1).orElse(2) // 1
none.orElse(2) // 2
Default value.
Print value to console.
opt(1).print() // logs 'Some:', '1'; returns Some(1)
opt(1).print('test') // logs '[test]', 'Some:', '1'; returns Some(1)
none.print('x') // logs '[x]', 'None'; returns None
Converts Opt to an array.
some(1).toArray() // [1]
none.toArray() // []
some(1).toString() // 'Some(1)'
none.toString() // 'None'
Create Opt instance from an array of one or zero items.
Opt.fromArray([]) // None
Opt.fromArray([1]) // Some(1)
Generated using TypeDoc