[英]How to convert a union of tuples to an object type
我有一个InputData
类型,它是string
或元组。 我想将其转换为 object。
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]
现在我的要求是上面的 map 到下面的 object 。
{username: string, password: string, isAbove18: boolean, photo: File}
我尝试了很长时间,但无法做出任何事情。 请帮我解决这个问题...
您可以使用带有键重命名的 object 映射:
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;
};
也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.