Skip to content

Instantly share code, notes, and snippets.

@ammarnajjar
Last active February 10, 2022 10:10
Show Gist options
  • Save ammarnajjar/ccb7da2bad89261a98ed8174dd3f15c2 to your computer and use it in GitHub Desktop.
Save ammarnajjar/ccb7da2bad89261a98ed8174dd3f15c2 to your computer and use it in GitHub Desktop.
create a typescript type that excludes everything but specific sub-type
interface SeatingGroup {
id: string;
name: string;
freeSeats: number;
}
interface Customer {
id: number;
name: string;
tag: string;
}
const PickedKeys = {
id: 'id',
name: 'name'
} as const;
type PickedKeys = keyof typeof PickedKeys;
type Picked<T, K extends Extract<keyof T, string>> = Pick<T, K>;
type SeatingGroupItem = Picked<SeatingGroup, PickedKeys>;
type CustomerItem = Picked<Customer, PickedKeys>;
const a: SeatingGroupItem = {
id: '',
name: ''
}
const b: CustomerItem = {
id: 1,
name: ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment