-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Generic type no longer narrowed as expected without extends object
#48468
Comments
This code previously worked for us in VS Code but is now causing around 11 errors in our codebase. Not sure if simply adding a |
extends object
CC @weswigham Looks like the following will work function deepEquals<T extends {} | null | undefined>(a: T, b: T): boolean {
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) {
return false;
}
return true;
} and so will function deepEquals<T extends object | null | undefined>(a: T, b: T): boolean {
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) {
return false;
}
return true;
} but not function deepEquals<T extends unknown>(a: T, b: T): boolean {
if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) {
return false;
}
if (Array.isArray(a) || Array.isArray(b)) {
return false;
}
if (Object.keys(a).length !== Object.keys(b).length) {
return false;
}
return true;
} Note that the first two only likely work due to @ahejlsberg's change in #43183. I don't know if we're willing to do something special for |
Also adds fixes for microsoft/TypeScript#48468
Also seeing this in the |
Playing around with a fix here FWIW |
The Generic type without constraint is vague, hence with the extends keyword you clearly state what you want that Generic type to be, which is the reason for Typescript. |
Bug Report
π Search Terms
π Version & Regression Information
β― Playground Link
Playground currently doesn't show the error:
Playground link
π» Code
π Actual behavior
The calls to
Object.keys
generate errors:π Expected behavior
No errors
The text was updated successfully, but these errors were encountered: