如何创建具有不同索引签名的Array派生?
例如
interface SaferArray<T> extends Array<T> {
[i: number]: T | undefined
}错误:
Interface incorrectly extends interface 'T[]'.
Index signatures are incompatible.
Type 'T | undefined' is not assignable to type 'T'. 发布于 2020-05-09 11:32:19
我会使用类型别名来完成此操作:
type SaferArray<T> = Array<T | undefined>;
const example: SaferArray<string> = ["hello"];
const a = example[0]; // string | undefined
const b = example[1]; // string | undefined如果您愿意,也可以进行扩展。您只是在要扩展的类型中缺少| undefined:
interface SaferArray<T> extends Array<T | undefined> {
[i: number]: T | undefined
}https://stackoverflow.com/questions/61691379
复制相似问题