0

在 TypeScript 中,您可以执行以下操作:

class Foo {
  constructor(a: string) {
    
  }
}

class Bar extends Foo {
  constructor(b: number) {
    super("hi");
  }
}

请注意,Bar更改Foo. 但是,如果我依赖 Foo 的任何子类来具有特定的构造函数签名怎么办?如何强制 TypeScript 不允许子类更改签名?

class Bar extends Foo {
  // Throw an error that the constructor signature cannot be changed
  constructor(b: number) {
    super("hi");
  }
}
4

0 回答 0