在下面的示例代码中,实现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) {}
}
这比一般看起来要复杂一些。有一个open suggestion。