假設我有一個型別
type A = {
a: string
b?: number
c: boolean
d?: string
}
我怎么能從中派生出一個我只需要的型別,a而b其他一切都是可選的
type A2 = {
a: string
b: number
c?: boolean
d?: string
}
我的方法是:
export type WithRequiredOnly<T, K extends keyof T> = T & { [P in K]-?: T[P] } & { [P in Exclude<keyof T, K>] ?: T[P] }
A2 = WithRequiredOnly<A, 'a'|'b'>
但這似乎仍然需要其他領域......
uj5u.com熱心網友回復:
type WithRequiredOnly<T, K extends keyof T> = Required<Pick<T, K>> & Partial<Omit<T, K>>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/514948.html
標籤:打字稿打字稿泛型
下一篇:即使使用警衛檢查,物件也可能為空
