Closed
Description
Bug Report
π Search Terms
Generic type narrowing
π Version & Regression Information
- This changed between versions 4.8.4 and 4.9.5
β― Playground Link
π» Code
function test<T extends any[] | Record<string, any>>(obj: T) {
if (Array.isArray(obj) || 'length' in obj) {
obj;
// ^? (parameter) obj: T extends any[] | Record<string, any>
} else {
obj;
// ^? (parameter) obj: never
}
}
function test2<T extends any[] | Record<string, any>>(obj: T) {
if (Array.isArray(obj)) {
obj;
// ^? (parameter) obj: T & any[]
} else {
obj;
// ^? (parameter) obj: T extends any[] | Record<string, any>
}
}
π Actual behavior
In the else branch of the test
function, obj is inferred as never
type.
π Expected behavior
In the else branch of the test
function, obj should be inferred as T extends any[] | Record<string, any>
type.