interface TypeA {
a: string
b: string
}
interface TypeB {
c: string
}
now i want to make a TypeC, a and b is optional, c is required.
interface TypeC {
a?: string
b?: string
c: string
}
are there any shortcut to make this? for example:
// type TypeC = TypeA? & TypeB
I think it's useful when TypeA is huge
playground
interface TypeA {
a: string
b: string
}
interface TypeB {
c: string
}
// First way using extends and Partial
interface TypeC extends Partial<TypeA>, TypeB {
// ...
}
// Second way using & and Partial
type TypeD = Partial<TypeA> & TypeB;
const variableC: TypeC = {
c: '',
};
const variableD: TypeD = {
c: '',
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With