Closed
Description
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> {
}