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
interfaceA{id: number;}constneverReturn1=(): never=>{thrownewError('A');};consttest1=(input: A|undefined): void=>{if(input===undefined){neverReturn1();}// Input can still be undefined according to TS.console.log(input.id);}constneverReturn2: ()=>never=(): never=>{thrownewError('A');};consttest2=(input: A|undefined): void=>{if(input===undefined){neverReturn2();}// Input is determined to be not undefined.console.log(input.id);}
🙁 Actual behavior
Error: Object is possibly 'undefined'. in test1.
🙂 Expected behavior
No errors, because neverReturn1() never returns.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
This is working as intended, see #32695. There are so many duplicates of this already. This limitation is for performance reasons.
A function call is analyzed as an assertion call or never-returning call when
the call occurs as a top-level expression statement, and
the call specifies a single identifier or a dotted sequence of identifiers for the function name, and
each identifier in the function name references an entity with an explicit type, and
the function name resolves to a function type with an asserts return type or an explicit never return type annotation.
Your search terms are rather excessive, no wonder you can't find a duplicate like this. I suggest to use a few related words as search terms, often including the modifier in:title. Definitely don't enter entire explanations and even code examples as the search terms.
🔎 Search Terms
When a function has return type
never
it can be used as a kind of falsy check:This does not work in all cases, as demonstrated on the playground link. Only when the const has an explicit type
never
is handled correctly.🕗 Version & Regression Information
It does not work in the any TypeScript version available at the PlayGround.
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=3.8.3#code/JYOwLgpgTgZghgYwgAgILIN4ChnOAEwC5kQBXAWwCNoBuLAXyywQHsQBnMEiAN2gCUIYUlBABGZAF5kACgCUxEL2hSAfJhzIwACygsA7t0MBRKHqgyA5Kkty69OszactEThOkzQAB1JhiqAA+pCD4EDCgEPgKyDwsBGoauMAwsj5+UpLSIWERStFJuNx8UILComLydLiMmgD0dcgAkiC+XAhwIMicwAA2vcjUyDnhkfjIiKxQ+KAA5losyAAqAMoAdJqsHCy9EGu9LLNerX5rBHYMTFsuSiVlIiAATMTyibcqnjHvUInYuDp6QxKExmFgWay2eyOa5cSCcR5SNInfxoYKhUb5GJxBKSdR-PCpY5tTLZdF5KJyQq4b73USPKqaWq4BrNZF4djIMKQKDkMYLQYoEAsLgjcn4Da4a47PYHI7pMBnaL2LBAA
💻 Code
🙁 Actual behavior
Error:
Object is possibly 'undefined'.
intest1
.🙂 Expected behavior
No errors, because
neverReturn1()
never returns.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: