gpt4 book ai didi

typescript - 如何为接口(interface)声明中不存在的属性指定类型

转载 作者:行者123 更新时间:2023-12-05 05:32:41 27 4
gpt4 key购买 nike

<分区>

所以我有一个具有一些标准属性名称和一些随机属性名称的对象。我想要一个强制已知属性类型的接口(interface)(比如说名字,年龄)并且还强制所有其他属性都是 bool 类型。这有可能吗?

interface IUser {
name: string;
age: number;
}
type IWishThisWouldWork = IUser & { [key: Exclude<string, keyof IUser>]: boolean }

const x: IWishThisWouldWork = {
name: 'john', //required and allow only string
age: 5, //required and allow only number
randomProp1: true, //allow only boolean
randomProp2: false //allow only boolean
}

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