[英]Typescript same key but different type nested (keyof but nested)
I am trying to created a custom type that replaces all types of a Generic parameter T to string[]
but keeps all the property names even nested.我正在尝试创建一个自定义类型,它将所有类型的通用参数 T 替换为
string[]
但保持所有属性名称甚至嵌套。
Expected behaviour: No error预期行为:没有错误
interface Bar {
lat: string,
lng: string
}
interface Foo {
id: string,
bar: Bar
}
export type PartialDeepKeyOf<T> = { [id in keyof T]: string[] }
const baz: PartialDeepKeyOf<Foo> = {
id: ['a','b'],
bar: {
lat: ['a','b'],
lng: ['a','b'],
}
}
Actual behaviour:实际行为:
Type '{ lat: string[];
输入 '{ lat: string[]; lng: string[];
液化天然气:字符串[]; }' is not assignable to type 'string[]'
}' 不可分配给类型 'string[]'
You need recursive type traversal你需要递归类型遍历
type PartialDeepKeyOf<T> = {
[P in keyof T]: T[P] extends string
? string[]
: PartialDeepKeyOf<T[P]>
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.