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
declarefunctiongetStringOrUndefined(): string|undefined;declarefunctionreceiveFrom(from: string): void;exportfunctiontest(mode: 'a'|'b'){constnarrowable=getStringOrUndefined();if(mode==="a"&&!narrowable){return;}// ...if(mode==="a"){receiveFrom(narrowable);// ~~~~~~~~~~// Argument of type 'string | undefined' is not assignable to parameter of type 'string'.// Type 'undefined' is not assignable to type 'string'.}}
π Actual behavior
narrowable is type string | undefined inside the second if.
π Expected behavior
We know that if mode === "a" then narrowable must be truthy. Otherwise the early return; would have been hit.
Additional information about the issue
I have a hunch I've seen this before, but searched through issues a bunch and couldn't find anything...
This is a simplified example from real-world code. The // ... contains some logic that must happen regardless of mode.
The text was updated successfully, but these errors were encountered:
π Search Terms
narrow union two values narrowing if condition
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwHMQMBlDGLVAgeRgFVVRFKRgAKASgC54BncygXgAfeGiYtgAbgBQM0JFgIU6bHnhwwILADcQAMRg4Atm0RHjPfhSrd4OnFmlyQADwAOOGBiRpMufAwQfjZjHFAeAHIoSJF4SIAjSI54AG8ZeHgwPH54VFgjAHcoBIgEAF5CYjIbGnpGEGZUVk5ZDPgsRHhQ8IryyoAiKAH4ADJR+ABCfJgikrKU9MzMuAxkGFRZTIBfOUyAen34ADpTvY6untB4fsHhxfaV8G09QxM2GbnSkA4t+F3tkA
π» Code
π Actual behavior
narrowable
is typestring | undefined
inside the secondif
.π Expected behavior
We know that
if mode === "a"
thennarrowable
must be truthy. Otherwise the earlyreturn;
would have been hit.Additional information about the issue
I have a hunch I've seen this before, but searched through issues a bunch and couldn't find anything...
This is a simplified example from real-world code. The
// ...
contains some logic that must happen regardless ofmode
.The text was updated successfully, but these errors were encountered: