-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this
Milestone
Description
Bug Report
π Search Terms
- optional chaining
π Version & Regression Information
It's broken in all tested versions of typescript (3.7 until 4.8 nightly in TS playground). Versions prior 3.7 do not support optional chaining.
β― Playground Link
Playground link with relevant code
π» Code
function foo(input: boolean[]) {
let acc: null | { prop?: boolean } = null
for (const item of input) {
acc = { prop: item || acc?.prop } // <- Property 'prop' does not exist on type 'never'.
}
}π Actual behavior
It infers never for acc variable, thus gives an error:
Property 'prop' does not exist on type 'never'.
function foo(input: boolean[]) {
let acc: null | { prop?: boolean } = null
for (const item of input) {
acc = { prop: item || acc?.prop } // <- Property 'prop' does not exist on type 'never'.
}
}π Expected behavior
To work and not to give an error (infer boolean for prop property) - the same way as does this equivalent code:
function foo(input: boolean[]) {
let acc: null | { prop?: boolean } = null
for (const item of input) {
acc = { prop: item || (acc !== null ? acc.prop : undefined) }
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this