Skip to content

OmitByType

题目

From T, pick a set of properties whose type are not assignable to U.

For Example

typescript
type OmitBoolean = OmitByType<
  {
    name: string;
    count: number;
    isReadonly: boolean;
    isEnable: boolean;
  },
  boolean
>; // { name: string; count: number }

解答

ts
type OmitByType<T, U> = {
  [P in keyof T as T[P] extends U ? never : P]: T[P];
};

Released under the MIT License.