【发布时间】:2019-12-22 11:49:48
【问题描述】:
就像在这个例子中一样,我不想选择一个属性并使整个事情成为可选的。但相反,我想将选择的属性设为可选。
目前我正在通过显式写{items: Partial<Item>[]}来解决这个问题。
有没有更聪明的方法?
interface Item{
id: number
name: string
}
interface User{
id: number
name: string
items: Item[]
}
type UserWithOptionalItemsItself = Pick<User, "id" | "name"> & Partial<Pick<User, "items">>
const x:UserWithOptionalItemsItself = {
id: 1,
name: "user name",
// items is optional.. this is not what I want
}
type UserWithOptionalItems = Pick<User, "id" | "name"> & {items: Partial<Item>[]}
const y:UserWithOptionalItems = {
id: 1,
name: "user name",
// you have to have items, but the properties are optional
items: [{
id: 123
}]
}
【问题讨论】:
标签: typescript typescript2.0 typescript-generics