typescript 允许类比实现的接口更具限制性

问题描述 投票:0回答:0

为什么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 的定义中出现编译错误。

typescript compiler-errors interface typeerror implements
© www.soinside.com 2019 - 2024. All rights reserved.