Skip to content

Strange error involving evolving array type and Array#sort callback #31728

Closed
@ajafff

Description

@ajafff

TypeScript Version: 3.6.0-dev.20190602 (seems to work with 20190601)

Search Terms:

Code

// @noImplicitAny: true
interface I {
    a: string;
    b: string;
}

declare function compare(a: I, b: I): number;

function test() {
    const result = [];
    result.push({foo: true, a: 'a', b: 'b'});
    //           ~~~~~~~~~ error here
    result.sort(compare); // commenting out this line makes the error disappear
}

Expected behavior:

No error

Actual behavior:

Argument of type '(a: I, b: I) => number' is not assignable to parameter of type '(a: { foo: boolean; a: string; b: string; }, b: { foo: boolean; a: string; b: string; }) => number'.
  Types of parameters 'a' and 'a' are incompatible.
    Type '{ foo: boolean; a: string; b: string; }' is not assignable to type 'I'.
      Object literal may only specify known properties, and 'foo' does not exist in type 'I'.

Maybe the error makes sense with strictNullChecks enabled (which is off in the above example), but then the location is completely off

Playground Link:

Related Issues:
The only two changes in that version are #31711 and #31687. /cc @ahejlsberg

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions