Instead of removing the property from a
, you can utilize destructured assignment to create a new object without that property:
const {c, ...b} = a;
Once this is done, b
will include all elements of a
except for c
.
If a
is defined as a certain type like { c: string, d: string }
, then the types of c
and b
will be automatically deduced as string
and { d: string }
respectively. However, using an Omit
type, as suggested by @Nurbol Alpybayev, would be more efficient than manually specifying the types.
To avoid any naming conflicts with another variable, you can rename c
using this syntax:
const {c: someOtherName, ...b} = a;
This method works well if you are aware of the property name during compilation. If the property name is not known beforehand, then TypeScript may not provide accurate assistances in determining the result type of the operation.
In such scenarios, it's better to define a
as { [k: string]: number }
so that delete a[c]
would suffice, or you can consider using the following approach:
const exclude = <T>(map: { [k: string]: T }, omitted: string[]): { [k: string]: T } =>
return Object.getOwnPropertyNames(a)
.filter(k => omitted.indexOf(k) >= 0)
.reduce((a, k) => (a[k] = map[k], a), {})
};
var b = exclude(a, ["c"]);