-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
type narrowing, typescript 4.3, generic, instance type spread, instance type spread in return object literal, spread operator cloning
π Version & Regression Information
this started when updating 4.3 beta
β― Playground Link
π» Code
interface Orange {
name: string;
}
interface Apple {
name: string;
}
function test<T extends Apple|Orange>(item: T): T {
// fails with: '{ name: string; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with
// a different subtype of constraint 'Orange | Apple'.(2322)
return {...item};
}
function test2<T extends Apple|Orange>(item: T): T {
const x = {...item};
// this works due to the indirection w/ a variable declaration resolving the type to `T`
return x;
}
π Actual behavior
TypeScript behaves inconsistently when the instance is cloned using spread operator, depending on
whether the spread operator is used in an object literal as variable initializer, or as direct value for a return
statement.
π Expected behavior
TypeScript should narrow the type consistently; so that no errors are reported inconsistently.
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