xxxxxxxxxx
type OriginalType = {
id: number;
name: string;
age: number;
};
// Remove the 'age' property from OriginalType
type NewType = Omit<OriginalType, 'age'>;
// Usage example
const originalObj: OriginalType = { id: 1, name: 'John', age: 30 };
const newObj: NewType = { id: 2, name: 'Jane' };
console.log(originalObj); // { id: 1, name: 'John', age: 30 }
console.log(newObj); // { id: 2, name: 'Jane' }
xxxxxxxxxx
type OriginalType = {
foo: number;
bar: string;
baz: boolean;
};
type TypeWithoutBar = Omit<OriginalType, 'bar'>;
// Test
const obj: TypeWithoutBar = {
foo: 42,
baz: true,
};
xxxxxxxxxx
To remove a property from an object in TypeScript,
mark the property as optional on the type and use the delete operator.
You can only remove properties that have been marked optional from an object.