【发布时间】:2021-02-10 15:12:50
【问题描述】:
我有以下类型定义作为助手:
type MapSchemaTypes = {
string: string;
number: number;
integer: number;
float: number;
boolean: boolean;
};
type MapSchema<T extends Record<string, keyof MapSchemaTypes>> = {
-readonly [K in keyof T]: MapSchemaTypes[T[K]]
}
有了它,我可以从一个简单的对象生成一个类型。例如:
const TestModel1 = {
id: "number",
lastname: "string",
firstname: "string",
valid: "boolean"
} as const;
type TestRecord1 = MapSchema<typeof TestModel1>;
结果正确:
type TestRecord1 = {
id: number;
lastname: string;
firstname: string;
valid: boolean;
}
但现在,我想从更复杂的对象生成完全相同的类型:
const TestModel2 = {
id: {
type: "number",
mapping: "_id",
defaultValue: 0
},
lastname: {
type: "string",
mapping: "_lastname",
defaultValue: ""
},
firstname: {
type: "string",
mapping: "_firstname",
defaultValue: ""
},
valid: {
type: "boolean",
mapping: "_valid",
defaultValue: false
}
} as const;
type TestRecord2 = MapSchema<typeof TestModel2>;
错误:
Type '{ readonly id: { readonly type: "number"; readonly mapping: "_id"; readonly defaultValue: 0; }; readonly lastname: { readonly type: "string"; readonly mapping: "_lastname"; readonly defaultValue: ""; }; readonly firstname: { ...; }; readonly valid: { ...; }; }' does not satisfy the constraint 'Record<string, "string" | "number" | "boolean" | "integer" | "float">'.
Property 'id' is incompatible with index signature.
Type '{ readonly type: "number"; readonly mapping: "_id"; readonly defaultValue: 0; }' is not assignable to type '"string" | "number" | "boolean" | "integer" | "float"'.
Type '{ readonly type: "number"; readonly mapping: "_id"; readonly defaultValue: 0; }' is not assignable to type '"float"'.ts(2344)
显然生成了错误的类型:
type TestRecord2 = {
id: unknown;
lastname: unknown;
firstname: unknown;
valid: unknown;
}
我不知道如何正确地做到这一点。我知道我必须更改 MapSchema 类型,但到目前为止我尝试的一切都失败了。
例如我试过:
type MapSchema<T extends Record<string, keyof MapSchemaTypes>> = {
-readonly [K in keyof T]: MapSchemaTypes[T[K]["type"]]
}
但它给了我以下错误:
Type 'T[K]["type"]' cannot be used to index type 'MapSchemaTypes'.ts(2536)
Type '"type"' cannot be used to index type 'T[K]'.ts(2536)
或者再次:
type MapSchema<T extends Record<string, keyof MapSchemaTypes>> = {
-readonly [K in keyof T]: MapSchemaTypes[T[K].type]]
}
错误:
Cannot find name 'type'.ts(2304)
是否有可能实现我想做的事情?
更新
在@Titian Cernicova-Dragomir very helpful answer 之后,这是我更新的类型助手:
type MapSchemaTypes = {
string: string;
number: number;
integer: number;
float: number;
boolean: boolean;
};
type MapSchemaDefaultValues = string | number | boolean | undefined | null;
type MapSchemaSimpleField = keyof MapSchemaTypes;
type MapSchemaComplexField = {
type: MapSchemaSimpleField;
mapping: string;
defaultValue: MapSchemaDefaultValues
};
type MapSchemaField = MapSchemaSimpleField | MapSchemaComplexField;
type MapSchemaDefinition = Record<string, MapSchemaField>;
type MapSchema<T extends MapSchemaDefinition> = {
-readonly [K in keyof T]: T[K] extends { type: infer TypeName } ? MapSchemaTypes[TypeName & MapSchemaSimpleField] : MapSchemaTypes[T[K] & MapSchemaSimpleField]
};
这让我可以做到以下几点:
const TestModel4 = {
id: "number",
lastname: {
type: "string",
mapping: "_lastname",
defaultValue: ""
},
firstname: "string",
valid: {
type: "boolean",
mapping: "_valid",
defaultValue: false
}
} as const;
type TestRecord4 = MapSchema<typeof TestModel4>;
而且结果类型是正确的:
type TestRecord4 = {
id: number;
lastname: string;
firstname: string;
valid: boolean;
}
【问题讨论】:
标签: typescript types type-conversion