How to omit fields in object spread?

If you don't mind an intermediate step, you can do something like this:

const { fieldF, ...everythingExceptF } = a;
const b: B = everythingExceptF;

This combines the spread and rest operators. The compiler seemed happy with this on my machine, and it gives me the result you'd expect.

Note: this will also create the constant fieldF, which you may or may not want. If you already need that name for some other reason in the same scope, you can reassign it like this:

const { fieldF: someOtherName, ...everythingExceptF } = a;

Tags:

Typescript