Skip to content

Call class function with generic arguments inside class gives error 2345 #41899

Closed
@aponomy

Description

@aponomy

TypeScript Version: 4.1.2

Search Terms:
(arg1:keyof T, arg2:T[typeof arg1])

Code

type MyType = {
    foo:string
}

class MyClass<T extends MyType = MyType> {

    /* this is the failing function */
    callMe = (arg1:keyof T, arg12:T[typeof arg1]) => null;

    /* callMe not working when calling inside the class */
    testInsideClass = () => {
        this.callMe('foo', 'not-working'); // <-- Compiler error on second argument
    }
}

/* callMe  working when calling from an instance of the class */
const classInstance = new MyClass<MyType>();
classInstance.callMe('foo', 'working');  // <-- No compiler error here

Expected behavior:
Able to call class function callMe from within the class since T extends MyType

Actual behavior:
Compiler says argument of type 'string' is not assignable to parameter of type 'T[keyof T]'.ts(2345)

Playground Link:

Related Issues:
Maybe #39945 is related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions