I feel in that case you should have some type union of `"notAsked" | "none" | { t: "some", v: "Name" }`, because that would be more clear of what each possible value means. Let `null` and `undefined` be synonyms for `nil`.
I feel in that case you should have some type union of `"notAsked" | "none" | { t: "some", v: "Name" }`, because that would be more clear of what each possible value means. Let `null` and `undefined` be synonyms for `nil`.
I actually created a `type Optional<T> = T | null` for one of my projects because I wanted a good way to describe something that is null vs undefined. This is specially important if you are de/serialising objects. In my case, undefined = user did nothing to it, null = user actually chose not to supply value (and yes, they are different)
You'll need to write undefined as a potential type for potentially undefined object members, and as such it means exactly that - undefined.
For everything else there is null.
A sanity check would be - undefined can only appear in a function parameter type or object member type as a union with other types, or in a === comparison. For everything else there is null.
Explicitly defining something as `undefined` is paradoxical and IMO should never be done. I've tried to explain this concept to some co-workers without much lasting success, but it's a pretty common concept to other languages.
Undefined should basically never be used as a value directly.
map.get("missingKey") returns undefined.
The new ?. operator returns undefined, even if you pass it null.
Undefined means that you don't have to care about the difference between { foo: undefined } and { }.
The only pain point is that JSON.parse() produces null, but this is relatively easy to work around.
I don't find any reason to ever produce a null. I just treat it as a value that can occur when using certain APIs.
If your code depends on the difference between a missing key and a missing value, probably you should be using a Map which makes that difference explicit with separate set() and delete() methods.