DeveloperBreeze

Utility Types Development Tutorials, Guides & Insights

Unlock 2+ expert-curated utility types tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your utility types skills on DeveloperBreeze.

TypeScript Generics and Advanced Types Cheatsheet: Master Complex Type Systems

Cheatsheet August 20, 2024
typescript

You can use the extends keyword to constrain a generic type to a subset of types that satisfy a particular condition.

function getProperty<T, K extends keyof T>(obj: T, key: K) {
  return obj[key];
}

const person = { name: 'Alice', age: 25 };
const name = getProperty(person, 'name'); // Works
// const invalid = getProperty(person, 'invalidKey'); // Error: Argument of type '"invalidKey"' is not assignable to parameter of type '"name" | "age"'.

Advanced TypeScript: Type Inference and Advanced Types

Tutorial August 05, 2024
typescript

interface Person {
  name: string;
  age: number;
}

interface Employee {
  employeeId: number;
}

type EmployeePerson = Person & Employee;

const john: EmployeePerson = {
  name: 'John Doe',
  age: 35,
  employeeId: 1234
};
function printId(id: number | string) {
  console.log('ID:', id);
}

printId(101);
printId('ABC123');