Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created October 9, 2022 01:57
Show Gist options
  • Save semlinker/e643c0ca8ca127b34a6459bfc9a697d8 to your computer and use it in GitHub Desktop.
Save semlinker/e643c0ca8ca127b34a6459bfc9a697d8 to your computer and use it in GitHub Desktop.
From T, pick a set of properties whose type are assignable to U.
type PickByType<T, U> = {
[P in keyof T as T[P] extends U ? P : never]: T[P];
};
type OnlyBoolean = PickByType<
{
name: string;
count: number;
isReadonly: boolean;
isEnable: boolean;
},
boolean
>; // { isReadonly: boolean; isEnable: boolean; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment