-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Assertions do not narrow related variables like conditions do #60904
Comments
See #44730.
|
@MartinJohns Thanks for the insights. To my understanding function assertions don't violate any of the rules from that definition. The conditional expression and the object/value are constants in this case, so they should be narrowed down, right? |
It works OK with a different kind of assertion function: function assertTrue(value: boolean): asserts value {
if (!value) throw Error("not true");
} The variant you have used ( |
@Andarist That made it work. Thanks. Just curious why doesn't |
It was designed to perform a different kind of narrowing. Only |
I see! Can I'm closing the issue since |
🔎 Search Terms
Assertion functions
Narrowing
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ssl=17&ssc=54&pln=1&pc=1#code/MYewdgzgLgBGCuBbAXHJAjApgJxgXhgEYBuAKFElgEsIA5JAeTE3zUXzwJNJ4HpeYAFQAWLAIIQIOKFXAxwAGwCecAIbZsIAO4QYAAxr1ETTHphQQMAERRs8TFYA0MdPFgLMqgG6ZdehIhmqroBWNikAGbwYMAycsFS2FCCdpgAFF6qCvao6CAgHqpgAJSoCdK6mdksNOapMADepDAwVBEwaQCEVfbF5sKaWjAAohog2GlWYCCwtvZWxWQAvqTlSSn2aYaMzIuk28bMxC0nLfwwqrHwWah6c6bHmAAeAA6YsZgAJrf3ZoCg5KQAsdTiCzgJLlBrgpbqEcHpHq93lAvrdCGYYIAZch4pHOegAkhEzGB1INdHkoMJ9AcTEEwJ99AE9Pt2l0tnQdphin0KYMRmMJlMZnV5ntqUdQTBzhCoT9UvCYM83h9vvpfjAAUCJSCpVcbvo0QilciVXo0RK-kA
💻 Code
🙁 Actual behavior
In the example above, when
isNumOne
is narrowed totrue
using an assertion functionassertTrue()
orif
, I expect num to be narrowed down to 1 at the same time because they're related.🙂 Expected behavior
This seems only true in the case of
if
.assertTrue()
narrows onlyisNumOne
without narrowingnum
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: