Oh man...I wish I could answer any "why" question about JS/TS...but all I know is that the TS transpiler doesn't complain when I do something like that without the | null 😂
I believe that setting an object type to null is fine, but undefined / unknown would cause an error.
Depends on whether you set the TS compiler to strict mode or not. I personally prefer having to add null or undefined to the type, so you can avoid checking for null-ish values before every function call, as long as you aren't writing library code at least.
-1
u/ef02 Jun 20 '21
The
| nullis unnecessary in TypeScript, but other than that, it seems fine.