在以下示例中,接口IFoo声明了一个需要两个数字参数的函数签名 . 抽象类BaseFoo实现此接口,但使用不同的签名声明该函数 . 最后,具体类Foo扩展了BaseFoo并实现了BaseFoo的函数声明版本 .

interface IFoo {
    func(x: number ): number
}

abstract class BaseFoo implements IFoo {
    abstract func(x: number): number
}

class Foo extends BaseFoo {
    func() { return -1 } // Does not match interface func declaration
}

let foo: IFoo = new Foo()  // Should not be able to instantiate a Foo as an IFoo
let y = foo.func()  // Should not be able to call without an argument
console.log(y)

这个人为的例子说明了现实生活中发生的事情:我在代码库中有一个现有的接口 . 我更新了其中一个函数的签名,期望编译器可以帮助我找到所有需要更新的类 . 但是,没有错误 .

为什么我允许实例化一个带有与接口不匹配的函数签名的抽象类?