我需要声明一个类似记录的类型,其中某些键具有覆盖值。
如下所示:
type Category = "circle" | "square" | "other"; // and 10+ other values
interface DataStore {
"circle": CircleData; // extends Data
"square": SquareData; // extends Data
[key in other Category values]: Data;
}
我不能在映射类型中添加/重新定义属性:
type DataStore = {
[P in Category]: Data;
circle: CircleData; // TS7061: A mapped type may not declare properties or methods.
square: SquareData;
}
发布于 2022-06-09 10:14:37
我想出的最好的解决办法是:
type SpecialDataStore = {
circle: CircleData;
square: SquareData;
}
type DataStore =
Record<Exclude<Category, keyof SpecialDataStore>, Data>
& SpecialDataStore;
https://stackoverflow.com/questions/72558563
复制相似问题