-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
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.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue