Closed
Description
Bug Report
π Search Terms
in operator, narrowing, generics, constraints, destructuring
π Version & Regression Information
- This changed between versions 4.3.5 and 4.4.2
β― Playground Link
Playground link with relevant code
π» Code
type X = { a: string } | { b: string }
// Works in 4.3.5 and 4.4.2
function func1<T extends X>(value: T) {
if ('a' in value) {
const a = value.a
console.log('a', a)
} else {
const b = value.b
console.log('b', b)
}
}
// Works in 4.3.5, not in 4.4.2
function func2<T extends X>(value: T) {
if ('a' in value) {
const { a } = value // Property 'a' does not exist on type 'X'.(2339)
console.log('a', a)
} else {
const { b } = value // Property 'b' does not exist on type 'X'.(2339)
console.log('b', b)
}
}
π Actual behavior
Types are not narrowed when using destructuring assignment and the compiler throws error 2339.
π Expected behavior
Types are narrowed when using destructuring assignment.