npm install --save option-t
const Some None = ;// `Some<T>`const some = 1;console; // trueconsole; // 1// `None`const none = ;console; // falseconsole; // this will throw `Error`.
Some types defines JSON representations if you serialize them by
These documents would provide more information about
These are written for Rust, but the essense is just same.
See the document.
Of course, there some alternative approaches. We introduce them.
From ECMA262 6th, we can use destructuring assignment. It provides a convinient way to handle/unwrap a value in an object.
This does same thing which is like a return value of
But this approach cannot call instance methods on their returned values.
If you would like to handle a result more seemless, we recommend to use
On the other hand, this way (and
option-t) need to allocate an object.
This allocation cost would be a cost.
const value = ; // this returns the actual value, otherwise `undefined`.if value !== undefined// handle some valueelse// handle none value
These approach don't need an extra object allocation like the above approach (and
And you need to think about "what is null type? including
undefined or not?".
At least in ECMA262, There are some ways to represent "there're no value".
Some static type checking tools provides a way to check nullability.
Flowtype and TypeScript checks with thier control flow analysis (Sorry, I don't know the details of Google Closure Compiler's behavior). Thus you can leave a runtime nullability checking in your code.
yarnto install dev-dependency toolchains.