3 ms·
Similar to why you would want to have both false and 0; it is possible to have a situation in which you wish to define something as explicitly being null. I do
by Crespyl 10y ago
Similar to why you would want to have both false and 0; it is possible to have a situation in which you wish to define something as explicitly being null.
I do think it's a little weird, but it's useful to be able to say that "this key exists and its value is null" as opposed to "there is no such key".
- SigmundA 10y agoI usually find it useful in API's, When you send object across undefined means no value was sent, null is a null value sent. That is the difference between not changing a value and changing it to null.
- Matthias247 10y agoI thought about that too, but this breaks down once you realize that setting obj.key = undefined; will mutate an object into something which is neither "the key exists and its value is null" nor "there is no such key". The difference can e.g. bite you in things that iterate over the keys of an object or function that do that internally (like deepEquals implementations). I guess in TS2 there would now also be a difference between a field defined as key: string | null | undefined and a field defined as key?: string | null | undefined, because the first one requires setting the key to one of the values on initialization and the other one not.