Skip to content

Generic type parameter is lost after generic member call #11584

@alexfoxgill

Description

@alexfoxgill

This is the simplest version of this bug I could find:

function example<T1>() {
    let x = {
        foo: <T2>(t2: T2) => x,
        bar: (t1: T1) => x
    }
    return x;
}

example<number>()
    .foo("hello")
    .bar(1) // Argument of type 'number' is not assignable to parameter of type 'T1'

The call to bar fails to compile. The type is example<number>, so it should compile.

If I try to add type information to x, another compilation error occurs:

type Example<T1> = {
    foo: <T2>(t2: T2) => Example<T1>,
    bar: (t1: T1) => Example<T1>
}
function example<T1>() {
    let x: Example<T1> = {
        foo: <T2>(t2: T2): Example<T1> => x,
        bar: (t1: T1): Example<T1> => x
    }
    return x;
}

example<number>()
    .foo("hello")
    .bar(1)

Type '{ foo: (t2: T2) => Example; bar: (t1: T1) => Example; }' is not assignable to type 'Example'.
Types of property 'foo' are incompatible.
Type '(t2: T2) => Example' is not assignable to type '(t2: T2) => Example'.
Type 'Example' is not assignable to type 'Example'.
Types of property 'bar' are incompatible.
Type '(t1: T1) => Example' is not assignable to type '(t1: T1) => Example'.
Types of parameters 't1' and 't1' are incompatible.
Type 'T1' is not assignable to type 'T1'.
let x: Example

Interestingly, removing either the foo or bar methods will allow the code to successfully compile:

type Example<T1> = {
    foo: <T2>(t2: T2) => Example<T1>,
    //bar: (t1: T1) => Example<T1>
}
function example<T1>() {
    let x: Example<T1> = {
        foo: <T2>(t2: T2): Example<T1> => x,
        //bar: (t1: T1): Example<T1> => x
    }
    return x;
}
type Example<T1> = {
    //foo: <T2>(t2: T2) => Example<T1>,
    bar: (t1: T1) => Example<T1>
}
function example<T1>() {
    let x: Example<T1> = {
        //foo: <T2>(t2: T2): Example<T1> => x,
        bar: (t1: T1): Example<T1> => x
    }
    return x;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions