microsoft / microsoft/TypeScript

Type with overloaded method with a generic overload matches type with incompatible method

Open
#37,263 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.9.0-dev.20200305

Search Terms:
generic type overload

Code

interface I1 {
    f<V extends 'a'>(value: V): void;
    f(value: 'b'): void;
}

interface I2 {
    f(value: 'c'): void;
}

function test(x: I1) {
    function u(y: I2) { }

    u(x);
}

Expected behavior:
Compilation error, u cannot accept x as I1 cannot handle f('c')

Actual behavior:
Compilation succeeds.
Note that all of the following fail, as expected:

interface I1 {
    // f<V extends 'a'>(value: V): void;
    f(value: 'b'): void;
}

interface I2 {
    f(value: 'c'): void;
}

function test(x: I1) {
    function u(y: I2) { }

    u(x);
}
interface I1 {
    f<V extends 'a'>(value: V): void;
    // f(value: 'b'): void;
}

interface I2 {
    f(value: 'c'): void;
}

function test(x: I1) {
    function u(y: I2) { }

    u(x);
}
interface I1 {
    f(value: 'a'): void;
    f(value: 'b'): void;
}

interface I2 {
    f(value: 'c'): void;
}

function test(x: I1) {
    function u(y: I2) { }

    u(x);
}

Playground Link:
https://www.typescriptlang.org/play/?ts=3.9.0-dev.20200305#code/JYOwLgpgTgZghgYwgAgJIEZkG8CwAoZQ5GAHgDVkIAPSEAEwGdkByOZgPgAoA3OAGwCuEAFzIyASlHcA9sDoBufEWI9+Q0cwBGzSchlzFeAL758oSLEQpUAJmxKiMVYJEsEOqbIX4TefDAEQBDBgaRBkSAYwTipRDHF7AkdA4NDwgU4ATzibBKxkXwdCDKpxQyMgA

Related Issues:
Not sure.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript 3.9.0-dev.20200305 compiler and the minimal interfaces and call in the issue, using the linked Playground example to reproduce the successful assignment. Confirm that the generic and non-generic overload combination incorrectly accepts I1 as I2, then add a regression test and verify that the call produces the expected compilation error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.