Skip to content

Instantly share code, notes, and snippets.

@Asatelit
Last active May 4, 2023 20:38
Show Gist options
  • Save Asatelit/e94519cb87ba4dc3f9adea1ea67b391a to your computer and use it in GitHub Desktop.
Save Asatelit/e94519cb87ba4dc3f9adea1ea67b391a to your computer and use it in GitHub Desktop.
TypeScript: Pick VS Record
// 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