如何从复杂对象的某些属性创建自定义类型?

时间:2020-10-28 09:15:26

标签: typescript types type-conversion

我有以下类型定义作为助手:

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;
}

1 个答案:

答案 0 :(得分:1)

如果您希望T的属性成为具有类型属性的对象,则对T的约束是错误的,应该使用Record<string, { type: keyof MapSchemaTypes }>

type MapSchema<T extends Record<string, { type: keyof MapSchemaTypes }>> = {
    -readonly [K in keyof T]: MapSchemaTypes[T[K]["type"]]
}

Playground Link

如果希望它既适用于复杂对象,又适用于类型名称,则可以在约束和条件类型中使用并集来区分这两个casse:

type MapSchema<T extends Record<string, { type: keyof MapSchemaTypes } | keyof MapSchemaTypes>> = {
    -readonly [K in keyof T]: T[K] extends { type: infer TypeName } ? MapSchemaTypes[TypeName &  keyof MapSchemaTypes] : MapSchemaTypes[T[K] & keyof MapSchemaTypes]
}

Playground Link