请参阅下面我想要的问题标题和伪代码如何替换任何?最好不要创建一些巨大的手动联合类型。
abstract class Letter {}
class A extends Letter {}
class B extends Letter {}
class C extends Letter {}
// potentially hundreds more "letters"
type LetterClass = any
const exampleUsage: LetterClass[] = [A, B, C]
尝试与typeof
type LetterClass = typeof Letter
const letters: LetterClass[] = [A, B, C]
const sample = letters[0]
new sample() // Cannot create an instance of an abstract class.
您可以使用这样的类型:{new(): Letter}
。完整的示例:
abstract class Letter {}
class A extends Letter {}
class B extends Letter {}
class C extends Letter {}
// potentially hundreds more "letters"
type LetterClass = {new(): Letter}
const letters: LetterClass[] = [A, B, C]
const sample = letters[0]
new sample()
你可以在typescript中使用typeof
关键字来为你的类创建一个类型。
type LetterClass = typeof Letter;