-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type predicate silently ignored when used as non-statement expression #61105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Another weird gotcha is that an IIFE causes the result of the assertion to be discarded if you use a parenthesized arrow function but NOT a braced arrow function. That is: function myAssert(a: boolean): asserts a{}
declare const x:boolean
(()=>{myAssert(x)})(); // will narrow the type of x
x
// ^?
// const x: true
declare const y:boolean;
(()=>(myAssert(y)))(); // won't narrow the type of x
y
// ^?
// const y: boolean |
This is working as intended. See #32695:
|
It seems that that #33622 tried to clean up some of the funky corner cases. E.g.: // fails to narrow, loudly (good!)
// "Assertions require the call target to be an identifier or qualified name.(2776)"
(myAssert || null)(a); But I'm not sure if that PR meant to flag assertion types that are ignored because they are not expression statements (and if not, why not). |
This was not narrowed in 3.7 (where #32695 was first introduced) but worked in 4.6, so I guess it got improved then while I can't find the specific PR. |
@RyanCavanaugh What's the design limitation? Is it impossible to flag when an |
It was mentioned in other issues that this is an intentional limitation done for performance reasons. |
Yes, that’s why expressions aren’t recursively explored during control-flow-analysis. not why they are dropped quietly during type analysis. If a compound expression contains a subexpression of |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
asserts, return, void
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?target=99&jsx=0&suppressImplicitAnyIndexErrors=false&ts=5.8.0-dev.20250202&ssl=19&ssc=1&pln=1&pc=1&q=5#code/GYVwdgxgLglg9mABAWwJ4EEDOmCmAnKACgEMAuRAIzjgBsdiwBKc47fKTRYxGTqPEDkQBvAFCIewRIQCExRoigALPHADuiMDg0BRPKryEARGDhRFAnDKOMA3KIC+o0aEiwEiHAA8AjCXJUtPRMIuIoGGwEJHZheDhQIHhIxPYSAPRpElkAegD8YRlZRdIADsR4xMjx+ApkFoKOzq7Q8EjeAEz+lNR0DApiEnTmXogAvOFYuFHyqYiFiNQlmDKIAJJc2DAA5loAJopKQnGYIDTmUHBciABu5TDEFHRcYPvKQlCoJUKsU+5IvJozIgIIcIABrHC7GSxeKJZKzeY5fLpTLFCSEMoVKpQGpcAI9YKNIA
💻 Code
🙁 Actual behavior
a
is not narrowed totrue
.This is odd because:
🙂 Expected behavior
I expect
a
to be narrowed totrue
.Additional information about the issue
It seems not all expressions cause the type assertion to be ignored.
With the comma operator, for instance, the type assertion is still respected:
With the
void
operator, the type assertion is ignored.Even when occurring as the parameter for an
await
,yield
, orreturn
statement, the type assertion is discarded.The text was updated successfully, but these errors were encountered: