If I have an expression and want to add extra properties without repeating existing ones, how can I achieve that?
For instance, if the expression is a variable, it's simple to include additional fields (like adding field e
):
const x = { a: 1 };
const y = x as (typeof x) & { e?: number };
But what if the variable (x
) is nested inside an object like this:
f({
x: { a: 1, b: 1, … }
// I want type of x to be { a: number; b: number; …; e?: number },
// but without repeating all properties
});
The key is not to have e
in the initial object for logic reasons, even as undefined
.
Although, assigning x.e
later on is necessary.