DeveloperBreeze

Generics Constraints Development Tutorials, Guides & Insights

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

Advanced TypeScript: Type Inference and Advanced Types

Tutorial August 05, 2024
typescript

type IsString<T> = T extends string ? 'string' : 'not string';

type Test1 = IsString<string>; // 'string'
type Test2 = IsString<number>; // 'not string'
type Action = 'create' | 'update' | 'delete';
type Entity = 'user' | 'post';

type LogMessage = `${Action}_${Entity}`;

function logAction(action: LogMessage) {
  console.log(`Logging action: ${action}`);
}

logAction('create_user'); // valid
logAction('update_post'); // valid
// logAction('read_user'); // error