README
Optional.ts
Optional.ts emulate Optional class of Java lang.
Instllation
To install Optinal.ts, use npm.
$ npm i @xiangma9712/optional.ts
Then import to your project.
import Optional from "@xiangma9712/optional.ts";
Dependency
TypeScript: >=2.8.1
Method Summery
|Modifier and Type|Method|Description|
|:-|:-|:-|
|static Optional<T>|empty<T>()|Returns an empty Optional.|
|Optional<T>|filter(predicate: (T)=> boolean)|If value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.|
|Optional<U>|flatmap(mapper: (T) => Optional<U>)|If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional.|
|T|get()|If a value is present, returns the value, otherwise throws 'No Such Element'|
|any|ifPresent(consumer: (T) => any)|If a value is present, invoke the specified consumer with the value, otherwise do nothing.|
|boolean|isPresent()|Return true if there is a value present, otherwise false.|
|Optional<U>|map(mapper: (T) => U)|If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.|
|Optinal<T>|of(value: NonNullable
Note
of(null)
doesn't throw an error
Unlike Java Optional,Optional.of(null)
doesn't throw an error. Since the argument ofof()
is defined asNonNullable<T>
, you will see the following error message in coding.
Argument of type 'null' is not assignable to parameter of type 'never'. ts(2345)
ifPresnt()
can return value
In Optional.ts, you can use function which returns value as a argument ofifPresent()
method.