-
Notifications
You must be signed in to change notification settings - Fork 12.8k
T extends unknown
is observably-different from an unconstrained T
#61242
New issue
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
Comments
My theory is that this is for backwards compatibility when symbol keys were being added. You can also get this behavior with any type whenever the apparent type of For example: const objectConstraint = <T extends { abc: number }>(x: Example<T>): string => x // Ok
const numberConstraint = <T extends 123>(x: Example<T>): string => x // Ok
// ^ `keyof 123` happens to be only strings so also okay. Notably this means that This does still leave |
#56652 seems possibly-relevant as well. |
This is indeed a back-compat allowance; for a long time the default constraint was closer to |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
"extends unknown" "type parameter" "generic" "conditional type" "keyof" "string" "assignable"
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?jsx=0#code/C4TwDgpgBAogHgQwLZgDYQDwBUB8UC8UWUEcwEAdgCYDOUA3gL5QD8UA1hCAPYBmRUAFxQawAE4BLCgHMAULIDG3CqKgBXCuwrcA7hQDCy0WIRTgBKNhJlKtdZu16cACjjD4yNJlwBKYcalpAjw4eSUVc21DCJMzC2wXN1hEFHQEvxFxQOCoOCA
💻 Code
🙁 Actual behavior
The
T extends unknown
version behaves differently from the unconstrained version.🙂 Expected behavior
The two versions should have the same behavior, probably both being rejected since code like this is unsafe:
Additional information about the issue
I was asked about this and eventually narrowed down the behavior to the example above.
#61203 is also possibly related?
The text was updated successfully, but these errors were encountered: