We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
This is the behavior in every version I tried, and I reviewed the FAQ for entries about returning a generic type
https://www.typescriptlang.org/play/?ts=5.5.3#code/GYVwdgxgLglg9mABKSAeAKogpgDylsAEwGdEBvRABwCc5KAuRARkQF8A+ACgENqBzRugCUg8gChEialighqSMhMmIAdGt58ANEsk06jAORg4UALRMD2yawDcY1mLEQExKIgBu3ADYgsiALzI4BCcFHoMzGxCdsqIAPRxiAB6APxiQA
function func<T extends { prop: 1 }>(arg: T): T { return { ...arg, prop: 'not-1', }; } const value = func({ prop: 1 }); // ^? { prop: 1 }
The function allows returning a wrong type, which results in an error in runtime
A compile error in the return statement
return
It actually started while me & @IdoBouskila were researching a similar bug (playground):
type Fruit = | { name: string; count: 1 | 2 | 3; available: true; } | { name: string; count: 0; } function func<T extends Fruit>(fruit: T): T { return { ...fruit, count: fruit.count + 1, }; } const value = func({ name: 'apple', count: 0 }); // ^? { name: 'apple', count: 0 }
In addition, when removing the return type, the inferred return type is still unexpected (playground):
function func<T extends { prop: 1 }>(arg: T) { return { ...arg, prop: 2, }; } const value = func({ prop: 1 }); const prop = value.prop; // ^? 1
I would've expected it to be number.
number
Am I missing something here?
The text was updated successfully, but these errors were encountered:
Duplicate of #50559 / #50185 / #10727. Used search terms: generic spread return in:title
generic spread return in:title
Sorry, something went wrong.
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
No branches or pull requests
🔎 Search Terms
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about returning a generic type
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.5.3#code/GYVwdgxgLglg9mABKSAeAKogpgDylsAEwGdEBvRABwCc5KAuRARkQF8A+ACgENqBzRugCUg8gChEialighqSMhMmIAdGt58ANEsk06jAORg4UALRMD2yawDcY1mLEQExKIgBu3ADYgsiALzI4BCcFHoMzGxCdsqIAPRxiAB6APxiQA
💻 Code
🙁 Actual behavior
The function allows returning a wrong type, which results in an error in runtime
🙂 Expected behavior
A compile error in the
return
statementAdditional information about the issue
It actually started while me & @IdoBouskila were researching a similar bug (playground):
In addition, when removing the return type, the inferred return type is still unexpected (playground):
I would've expected it to be
number
.Am I missing something here?
The text was updated successfully, but these errors were encountered: