Skip to content

Regression in Type Inference engine in TypeScript 2.7.1 #22376

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

Closed
benny-medflyt opened this issue Mar 7, 2018 · 3 comments
Closed

Regression in Type Inference engine in TypeScript 2.7.1 #22376

benny-medflyt opened this issue Mar 7, 2018 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@benny-medflyt
Copy link

The following code works great in these TypeScript versions:

  • 2.4.2
  • 2.5.3
  • 2.6.1
  • 2.6.2

But it gives a compile error in these TypeScript versions:

  • 2.7.1
  • 2.7.2
  • 2.8.0-dev.20180307

BugTest.ts

class Write {
    protected dummy: Write;
}

class Col<s, a> {
    protected dummy: [Col<s, a>, s, a];
}

class Table<Req, Def> {
    protected dummy: [Table<Req, Def>, Req, Def];
}

type MakeTable<T1 extends object, T2 extends object> = {
    [P in keyof T1]: Col<Write, T1[P]>;
} & {
        [P in keyof T2]: Col<Write, T2[P]>;
    };

class ConflictTarget<Cols> {
    public static tableColumns<Cols>(cols: (keyof Cols)[]): ConflictTarget<Cols> {
        return <any>{
            tableCols: cols.slice()
        };
    }

    protected dummy: [ConflictTarget<Cols>, Cols];
}



const bookTable: Table<BookReq, BookDef> = null as any

interface BookReq {
    readonly title: string;
    readonly serial: number;
}

interface BookDef {
    readonly author: string;
    readonly numPages: number | null;
}


function insertOnConflictDoNothing<Req extends object, Def extends object>(_table: Table<Req, Def>, _conflictTarget: ConflictTarget<Req & Def>): boolean {
    throw new Error();
}

function f() {
    insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns(["serial"]));  // <-- Compile error here
}
BugTest.ts(49,42): error TS2345: Argument of type 'ConflictTarget<{ serial: any; }>' is not assignable to parameter of type 'ConflictTarget<BookReq & BookDef>'.
  Type '{ serial: any; }' is not assignable to type 'BookReq & BookDef'.
    Type '{ serial: any; }' is not assignable to type 'BookReq'.
      Property 'title' is missing in type '{ serial: any; }'.

Manually adding the type argument allows the compile to succeed:

insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns<BookReq & BookDef>(["serial"]));
@RyanCavanaugh
Copy link
Member

Looks like a change in inference priority - @ahejlsberg any thoughts?

@ahejlsberg
Copy link
Member

Adding @sandersn. Looks like it is caused by #21271. We now infer from ["serial"] to (keyof Cols)[], producing the candidate { serial: any }. This candidate is given higher priority than the inference we make from the return type. Probably it should have lower priority, particularly since it is known to be incomplete (in that we don't have specific types for the properties).

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Mar 7, 2018
@ahejlsberg ahejlsberg added this to the TypeScript 2.8 milestone Mar 7, 2018
sandersn added a commit to weswigham/TypeScript that referenced this issue Mar 14, 2018
@sandersn
Copy link
Member

Fixed in #22525

@sandersn sandersn added the Fixed A PR has been merged for this issue label Mar 14, 2018
weswigham added a commit that referenced this issue Mar 19, 2018
* Combine keyof T inferences

* Extract covariant inference derivation into function

* Test:keyof inference lower priority than return inference

for #22376

* Update 'expected' comment in keyofInferenceLowerPriorityThanReturn

* Update comment in test too, not just baselines

* Fix typo

* Move tests
DanielRosenwasser pushed a commit that referenced this issue Mar 20, 2018
* Combine keyof T inferences

* Extract covariant inference derivation into function

* Test:keyof inference lower priority than return inference

for #22376

* Update 'expected' comment in keyofInferenceLowerPriorityThanReturn

* Update comment in test too, not just baselines

* Fix typo

* Move tests
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants