Closed
Description
🔎 Search Terms
Assertion functions
Narrowing
🕗 Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
const num: number = 1;
const isNumOne = num === 1;
// The Assertion only narrows `isNumOne` to "true", but leaves `num` as number
function assertTrue(value: boolean): asserts value is true {
if (!value) throw Error("not true");
}
assertTrue(isNumOne);
isNumOne; // actual: `true`; expected: `true` ✅
num; // actual: `number`; expected: `1` ❌
// `If` narrows both `isNumOne` and `num`
if (!(isNumOne)) throw Error("not true");
isNumOne; // actual: `true`; expected: `true` ✅
num; // actual: `1`; expected: `1` ✅
🙁 Actual behavior
In the example above, when isNumOne
is narrowed to true
using an assertion function assertTrue()
or if
, 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 only isNumOne
without narrowing num
.
Additional information about the issue
No response