【发布时间】:2017-06-11 17:51:44
【问题描述】:
我将我的泛型类型定义为
interface IDictionary<TValue> {
[key: string|number]: TValue;
}
但是 TSLint 在抱怨。我应该如何定义一个可以作为键的对象索引类型?我也尝试了这些,但没有运气。
interface IDictionary<TKey, TValue> {
[key: TKey]: TValue;
}
interface IDictionary<TKey extends string|number, TValue> {
[key: TKey]: TValue;
}
type IndexKey = string | number;
interface IDictionary<TValue> {
[key: IndexKey]: TValue;
}
interface IDictionary<TKey extends IndexKey, TValue> {
[key: TKey]: TValue;
}
以上都不起作用。
那怎么办?
【问题讨论】:
标签: typescript typescript-generics