remove attribute from type in typescript

To remove an attribute from a type in Typescript, you can use the Omit utility type, which creates a new type by omitting the specified attributes from the original type. Here's an example:

index.ts
type MyType = {
  foo: string;
  bar: number;
  baz: boolean;
};

type MyTypeWithoutBar = Omit<MyType, 'bar'>;

// MyTypeWithoutBar is now { foo: string; baz: boolean; }
170 chars
10 lines

In this example, we created a type MyType with three attributes (foo, bar, and baz). We then created a new type MyTypeWithoutBar by using the Omit utility type to omit the bar attribute from MyType.

You can also use the Pick utility type to create a new type that only includes the specified attributes from the original type:

index.ts
type MyTypeWithOnlyFooAndBaz = Pick<MyType, 'foo' | 'baz'>;

// MyTypeWithOnlyFooAndBaz is now { foo: string; baz: boolean; }
126 chars
4 lines

In this example, we created a new type MyTypeWithOnlyFooAndBaz by using the Pick utility type to only include the foo and baz attributes from MyType.

gistlibby LogSnag