【发布时间】:2021-06-06 18:43:12
【问题描述】:
我想强制所有方法返回相同的类型。
像这样:
interface ITextGenerator {
[key: string]: () => string
}
class TextGenerator implements ITextGenerator {
genarator1 = () => "text1"
genarator2 = () => "text2
}
TextGenerator 中的错误:
“TextGenerator”类错误地实现了“ITextGenerator”接口。 “TextGenerator”类型中缺少索引签名。
【问题讨论】:
-
您可以像这样将索引签名添加到类中:tsplay.dev/mMyvZm
标签: typescript