I have one interface with many fields and other interface with almost all fields (just one less).
How in TypeScript without untyped unsafe crutches (<>
, as
, any
, unknown
and similar) can one exclude fields in object spread (so without enumerating every field I want to copy)?
interface A {
fieldA: number;
fieldB: string;
fieldC: number;
fieldD: string;
fieldE: number;
fieldF: string;
}
interface B {
fieldA: number;
fieldB: string;
fieldC: number;
fieldD: string;
fieldE: number;
// no fieldF here
}
const a: A = {
fieldA: 1,
fieldB: 'b',
fieldC: 3,
fieldD: 'd',
fieldE: 5,
fieldF: 'f'
}
const b: B = {
...a,
fieldF: undefined
};
This doesn't compile, because fieldF is still present, only have a value of undefined
.