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 realised that the most compelling use-case of narrowing unknown by === is for a selection of strings/enums, but IMO, this is cleaner when written as a switch.
Code
enumSomeResponse{Yes,No,Maybe,}functionfoo(x: unknown): SomeResponse{switch(x){caseSomeResponse.Yes:
caseSomeResponse.No:
caseSomeResponse.Maybe:
returnx;default:
thrownewError("I don't know. Can you repeat the question?");}}
Expected behavior:
No error.
Actual behavior:
Error on return statement: unknown is not assignable to SomeResponse
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms: switch narrow unknown if
I realised that the most compelling use-case of narrowing
unknown
by===
is for a selection of strings/enums, but IMO, this is cleaner when written as a switch.Code
Expected behavior:
No error.
Actual behavior:
Error on return statement:
unknown
is not assignable toSomeResponse
Playground Link: playground
Related Issues: #25720 #25172
The text was updated successfully, but these errors were encountered: