如何仅从接口获得数组或其他类型?
我有这样的接口:
interface Car {
model: string;
owners: string[];
}
我只想获得数组属性:
type NewCar = OnlyArrays<Car>
这将等于:
type NewCar = {
owners: string[];
}
键入并不重要,例如数组。
我尝试了此代码,但它不起作用:
function getCar<T = Car>(id: string): { [P in keyof T]: T[P] extends array ? string[] : string; };
I have an interface like that:
interface Car {
model: string;
owners: string[];
}
And I want to get only array properties:
type NewCar = OnlyArrays<Car>
And this will be equal to:
type NewCar = {
owners: string[];
}
Type it's not important, array just for example.
I've tried this code but it doesn't work:
function getCar<T = Car>(id: string): { [P in keyof T]: T[P] extends array ? string[] : string; };
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以声明映射类型并重新重新键( docs )仅包含该值扩展数组的那些键:
”
You can declare a mapped type and remap the keys (docs) to include only those keys for which the value extends an array:
TypeScript playground