为什么typescript允许MyClass在someMethod的参数类型上比它实现的接口更严格?
interface MyInterface { someMethod(n: number|Date):string }
class MyClass implements MyInterface {
someMethod( d: Date ): string { return d.toDateString(); } // why is this accepted ?
}
function hi( p: MyInterface ): string
{
return p.someMethod(10);
}
hi( new MyClass() ); // this will fail at runtime
我期待在 someMethod 的定义中出现编译错误。