Last active
February 10, 2022 10:10
-
-
Save ammarnajjar/ccb7da2bad89261a98ed8174dd3f15c2 to your computer and use it in GitHub Desktop.
create a typescript type that excludes everything but specific sub-type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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