Bug Report
🔎 Search Terms
narrow in operator
in operator
narrow operator
operator type guard
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about the "in" operator.
Tested on all TS versions available in the playground (at the time of writing: 3.3 - Nightly of 4.2.0)
⏯ Playground Link
Playground link with relevant code
💻 Code
const keywordA = 'a'
const keywordB = 'b'
type A = { [keywordA]: number }
type B = { [keywordB]: string }
declare const c: A | B
if ('a' in c) {
c // narrowed to `A`
}
if (keywordA in c) {
c // not narrowed, still `A | B`
}
🙁 Actual behavior
When using a const string as the "prop" argument for the in operator the type of the object is not narrowed.
🙂 Expected behavior
Since I'm using a const, thus dealing with a string literal type (same as when using a literal value as an argument), I'd expect the type to be narrowed inside the if block.
Bug Report
🔎 Search Terms
narrow in operatorin operatornarrow operatoroperator type guard🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about the "in" operator.
Tested on all TS versions available in the playground (at the time of writing: 3.3 - Nightly of 4.2.0)
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
When using a
conststring as the "prop" argument for theinoperator the type of the object is not narrowed.🙂 Expected behavior
Since I'm using a const, thus dealing with a string literal type (same as when using a literal value as an argument), I'd expect the type to be narrowed inside the
ifblock.