我想将每种类型都转换为boolean
或object
type CastDeep<T, K = boolean> = {
[P in keyof T]: K extends K[]
? K[]
: T[P] extends ReadonlyArray<K>
? ReadonlyArray<CastDeep<K>>
: CastDeep<T[P]>
}
interface ITest {
city: {
name: string,
}
}
预期结果:
excludeProps<ITest>({
city: true,
});
要么
excludeProps<ITest>({
city: {
name: true
},
});
当前错误信息:
19 name: string,
~~~~
The expected type comes from property 'name' which is declared here on type 'CastDeep<{ name: string; }, boolean>'
最佳答案
好,找到解决方法
export type ICastDeep<T> = {
[P in keyof T]: boolean | ICastDeep<T[P]>;
}