34
loading...
This website collects cookies to deliver better user experience
null
or undefined
.strictNullChecks
The value can be null
or undefined
. In the end, these are both valid JavaScript primitives and people can use them in many ways.
You want to add some advanced logic. Writing x == null
everywhere gets cumbersome.
None
on Nothing
) or there is a value (Some
or Just
).tag
that defines what possibility it is.type Option<T> = { tag: 'none' } | { tag: 'some', value: T }
null
(or undefined
) is global. It is one value equal to itself. It is the same for everybody. null
and I return null
, later, it is not possible to find out where the null
comes from.null
.const None = Symbol(`None`)
null
. See some example use:const isNone = (value: unknown) => x === None
const hasNone = (arr: Array<unknown>) =>
arr.some((x) => x === None)
const map = <T, S>(
fn: (x: T) => S,
value: T | typeof None
) => {
if (value === None) {
return None
} else {
return fn(value)
}
}
undefined
.JSON.stringify({ x: Symbol(), y: undefined })
// -> "{}"
JSON.stringify([Symbol(), undefined])
// -> "[null,null]"
undefined
—the native ‘missing value’)—makes it well suited for representing a custom ‘missing value’.tag
set to none
is considered None. This allows for easier serialisation and deserialisation.null
in places where no advanced operations on the property are needed.const notSettled = Symbol(`not-settled`)
Let me know what you think of this use? Is it a good replacement for null
? Should everybody always use an Option?