Assume I have a type
type A = {
a: string
b?: number
c: boolean
d?: string
}
How could I derive a type from it where I only require a
and b
and leave everything else optional
type A2 = {
a: string
b: number
c?: boolean
d?: string
}
My approach was:
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'>
But this seems to still require the other fields...
CodePudding user response:
type WithRequiredOnly<T, K extends keyof T> = Required<Pick<T, K>> & Partial<Omit<T, K>>