Skip to content

Generic type no longer narrowed as expected without extends objectΒ #48468

Closed
@mjbvz

Description

@mjbvz

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

  • Regression in: 4.7.0-dev.20220325
  • Works in 4.7.0-dev.20220323

⏯ Playground Link

Playground currently doesn't show the error:

Playground link

πŸ’» Code

function deepEquals<T>(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) { // Error here
        return false;
    }
    return true;
}

πŸ™ Actual behavior

The calls to Object.keys generate errors:

No overload matches this call.
  Overload 1 of 2, '(o: {}): string[]', gave the following error.
    Argument of type 'T' is not assignable to parameter of type '{}'.
  Overload 2 of 2, '(o: object): string[]', gave the following error.
    Argument of type 'T' is not assignable to parameter of type 'object'.

πŸ™‚ Expected behavior

No errors

Metadata

Metadata

Labels

Breaking ChangeWould introduce errors in existing codeFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions