1. The satisfies Operator: Safe Validation with Exact Types
The satisfies operator validates that an expression matches a type, without widening the resulting inferred type.
js
type ThemeColors = 'primary' | 'secondary' | 'accent';
const palette = {
primary: '#7c3aed',
secondary: '#a855f7',
accent: [124, 58, 237],
} satisfies Record<ThemeColors, string | number[]>;
// TypeScript preserves exact string type for palette.primary
const primaryHex = palette.primary.toUpperCase();
2. Template Literal Types for Safe Routing
js
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
type ApiRoute = '/users' | '/posts' | '/comments';
type ApiEndpoint = `${HttpMethod} ${ApiRoute}`;
function callApi(endpoint: ApiEndpoint) {
console.log(`Executing ${endpoint}`);
}
// Full autocomplete and type-safety
callApi('GET /posts');
3. Custom Type Predicates for Safe Narrowing
js
interface Article {
title: string;
slug: string;
}
function isArticle(item: any): item is Article {
return item && typeof item.title === 'string' && typeof item.slug === 'string';
}
