Golden Codes - armanexplorer planet

Practical code snippets for Django, Python, Bash, Git and All!

View on GitHub

Define custom type using object

const person = {
  name: 'John',
  age: 30,
  location: 'New York'
};

type PersonKeys = keyof typeof person;

// PersonKeys is the same as "name" | "age" | "location"
type Person = {
  [K in PersonKeys]: K extends 'age' ? number : string;
};

// Person is the same as { name: string, age: number, location: string }