gpt4 book ai didi

javascript - 如何将属性转换为 bool 值(深)

转载 作者:搜寻专家 更新时间:2023-10-30 21:09:38 28 4
gpt4 key购买 nike

我想将每种类型都转换为booleanobject

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

关于javascript - 如何将属性转换为 bool 值(深),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56390113/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com