Skip to content

[BUG] Type parameter that declared by "extends of" should not be a required type parameter. #39754

Closed
@yaquawa

Description

@yaquawa

Here is the code I've got an error.

class Foo<T, K extends keyof T> {
    sayHi(name: K | K[]) {
        console.log(`hi ${name}`);
    }
}

interface Event {
    click(element: string, event: object):void
    touch(element: string, event: object):void
}

const f = new Foo<Event>(); // TS2558: Expected 2 type arguments, but got 1.

Where K is a type that is determined by T automatically, so typescript compiler should not allow users to specify the K type parameter manually.

Did I miss any use cases that a user should specify K manually?

If the K can be an "optional type parameter" that can be specified by user manually , the format should be this I suppose.

class Foo<T, K = keyof T> {
}

By now I'm using this format to avoid the error. But I think this is so pointless..

class Foo<T, K extends keyof T = keyof T> {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions