-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsFix AvailableA PR has been opened for this issueA PR has been opened for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
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
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.
a-tarasyuka-tarasyuk
Metadata
Metadata
Assignees
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsFix AvailableA PR has been opened for this issueA PR has been opened for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript