我有一个InputData
是要么string
是元组的类型。我想将其转换为对象。
type InputType = File | string | boolean;
type InputData = string | [string, InputType]
type test = "username" | "password" | ["isAbove18", boolean] | ["photo", File];
在上面的类型中,仅string
(给出输入字段的名称)的类型应设置为string
。我有通用类型
type AddDefaultValues<Data extends InputData> = Data extends string ? [Data, string] : Data
//AddDefaultValues<test> = ["username", string]| ["password", string] | ["isAbove18", boolean] | ["photo", File]
现在,我的要求是将上述类型映射到如下所示的对象。
{username: string, password: string, isAbove18: boolean, photo: File}
我已经尝试很长时间了,但是什么都做不了。请帮助我...
您可以将对象映射与键重命名一起使用:
type test = "username" | "password" | ["isAbove18", boolean] | ["photo", File];
type ObjMap = {
[K in test as K extends [infer F, infer _] ? F : K]: K extends [infer _, infer S] ? S : string;
};
也可以看看:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句