Hacker News new | ask | show | jobs
by ht85 2491 days ago
Could you give an example of the kind of problem you're talking about?
1 comments

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.
Doesn'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) ...)
Sounds 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.