Closed
Description
Bug Report
🔎 Search Terms
const assertion
const narrow
assertion narrow
🕗 Version & Regression Information
First seen in 4.1.2, reproduced in 4.4.3 and 4.5.0-dev.20210919.
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Type System Behavior", "Type Guards", "Things That Don't Work"
⏯ Playground Link
Playground link with relevant code
💻 Code
const test: { myfield?: number } = {};
const constField = 'myfield' as const;
if (test[constField]) {
console.log(test[constField] + 1); // Object is possibly 'undefined'
console.log(test.myfield + 1); // Object is possibly 'undefined'
}
🙁 Actual behavior
TypeScript is not able to narrow test[constField]
from undefined|number
to number
🙂 Expected behavior
TypeScript infers that test[constField]
is simply number
in the if
block
Metadata
Metadata
Assignees
Labels
No labels