3 ms·
I am trying to remember the error that I got. I think it was complaining that the variable being passed to a function may be a null rather than the type that wa
by collyw 7y ago
I am trying to remember the error that I got. I think it was complaining that the variable being passed to a function may be a null rather than the type that was specified.
- SenHeng 7y agoSounds like an optional variable. function fn(arg?: SomeObject) Or worst still, an or undefined, often found in class variables. this.foo: Thing | undefined = undefined; Easy way to sidestep this error is > if (!arg) return;, but it’s really a code smell of a larger, design issue. As in, the function shouldn’t really be depending on an optional value.
- tuxracer 7y agoDoesn't sound like a problem you get with typing, sounds like it was actually catching what could be a runtime error in production. If the variable is possibly null but the function you're passing it to expects it to not be null then typescript is pointing out what could be a runtime error if you don't explicitly handle that case (if (!foo) ...)