null vs undefined in TypeScript #825
-
I started working on enabling typescript strictNullChecks. As expected, I'm running into TS errors like this:
This error makes me wonder what sort of patterns we want to establish for using Personally, I've struggled with For example: [0].find(i => i > 1) // undefined
"foo".match(/[A-Z]/) // null I'm aware of various analogies that attempt to make sense of For example:
I tend to prefer using As we work to make our types more strict, I'm wondering how we want to approach this problem of I'm hoping to avoid code with function yuck(): string | null | undefined {
//...
} @pavish I'd love to hear your thoughts. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I like that you're bringing this up and I think that enabling [0] https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/ |
Beta Was this translation helpful? Give feedback.
-
I think we should go with We would mostly only want to worry about serialization for handling requests and responses, where In short, for type definitions and return types of functions, we would prefer |
Beta Was this translation helpful? Give feedback.
@seancolsen @dmos62
I think we should go with
undefined
. I agree on the complexity it'll bring if we use bothnull
andundefined
.undefined
is the most common return value for a number of inbuilt functions and I think it's more convenient to use it.We would mostly only want to worry about serialization for handling requests and responses, where
null
would be essential, but I do not see any other case where we might requirenull
.In short, for type definitions and return types of functions, we would prefer
undefined
.null
will only be used where it is strictly necessary such as in requests and responses.