在下面的示例代码中,实现MyInterface
...
any
如果可以检测到错误的类型,为什么不能推断出正确的类型?
interface MyInterface<T = any> {
myFunction(input: T): void;
}
class MyClass implements MyInterface<number> {
// input inferred as 'any'
myFunction(input) {}
}
class MyClass2 implements MyInterface<number> {
// Type 'number' is not assignable to type 'string'
myFunction(input: string) {}
}