Closed
Description
TypeScript Version: 3.3.3.3333
Search Terms:
type guard, union type of objects, typeof
Code
function fn(i: { foo: string, bar: number, baz: boolean } | { foo: number, bar: boolean }) {
// Notice: i.foo has two different types in intelligence and after written down.
if (typeof i.foo === 'string') {
// as screenshot, in intelligence, 'i.foo' is of type 'string | number'
i.foo // after written down, i.foo becomes 'string'
i.bar // always 'number | boolean'
}
}
Code 2
function fn(i: { foo: string, bar: number, baz: boolean } | { foo: number, bar: boolean }) {
// NOTICE: we added parenthesis
if ((typeof i.foo) === 'string') {
i.foo // string | number
i.bar // number | boolean
}
}
Expected behavior:
in 'if' branch, ‘i’ should be { foo: string, bar: number, baz: boolean }
Actual behavior:
As comments says.