Last active
May 4, 2023 20:38
-
-
Save Asatelit/e94519cb87ba4dc3f9adea1ea67b391a to your computer and use it in GitHub Desktop.
TypeScript: Pick VS Record
This file contains 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
// Definition of types | |
type Pick<T, K extends keyof T> = { | |
[P in K]: T[P]; | |
} | |
type Record<K extends string, T> = { | |
[P in K]: T; | |
} | |
// PICK | |
interface Dog { | |
id?: number; | |
name: string; | |
weight: number; | |
age: number; | |
} | |
type Doggy = Pick<Dog, 'id' | 'name' >; | |
// is equivalent to | |
type Doggy = { | |
id?: number; | |
name: string; | |
} | |
// RECORD | |
type ThreeDogProps = Record<'prop1' | 'prop2' | 'prop3', Dog>; | |
// is equivalent to | |
type ThreeDogProps = { | |
prop1: Dog; | |
prop2: Dog; | |
prop3: Dog; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment