You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a formatting function, where the input should always be a string, but in certain cases, from the backend, it's undefined for some reason. So to make sure the front-end at least doesn't crash when that happens, I added optional chaining, which fixed the issue.
But the types are kind of off now, and I didn't realize until now because Typescript didn't complain about anything.
Expected behavior: Should get an error/warning about something here. Preferably, I guess optional chaining should always expand the type to include undefined to whatever it is added to? Then, in this example, Typescript could say that the return type is string, but I'm trying to return string | undefined? Either that, or maybe it shouldn't be possible to use optional chaining on a value that does not include either the type undefined and/or null?
Actual behavior: No warnings or errors.
Related Issues: no
The text was updated successfully, but these errors were encountered:
RyanCavanaugh
changed the title
Optional Chaining is not checked properly?
Optional chaining should always include undefined in its resolved type
Feb 10, 2020
TypeScript Version: 3.7.5
Search Terms: optional chaining return type
Code
I have a formatting function, where the input should always be a string, but in certain cases, from the backend, it's
undefined
for some reason. So to make sure the front-end at least doesn't crash when that happens, I added optional chaining, which fixed the issue.But the types are kind of off now, and I didn't realize until now because Typescript didn't complain about anything.
Expected behavior: Should get an error/warning about something here. Preferably, I guess optional chaining should always expand the type to include
undefined
to whatever it is added to? Then, in this example, Typescript could say that the return type isstring
, but I'm trying to returnstring | undefined
? Either that, or maybe it shouldn't be possible to use optional chaining on a value that does not include either the typeundefined
and/ornull
?Actual behavior: No warnings or errors.
Related Issues: no
The text was updated successfully, but these errors were encountered: