Five TypeScript Tips I Wish I Knew Earlier
A few patterns that I keep reaching for.
1. satisfies instead of type assertion
const config = {
port: 3000,
host: 'localhost',
} satisfies ServerConfig;
Unlike a type assertion, satisfies still infers the narrowest type, so config.port is 3000 not number.
2. Discriminated unions over optional fields
// Avoid
type Result = { ok: boolean; value?: string; error?: string };
// Prefer
type Result = { ok: true; value: string } | { ok: false; error: string };
The second form lets TypeScript narrow correctly inside an if (result.ok) check.
3. as const on lookup tables
const STATUS = { active: 'active', archived: 'archived' } as const;
type Status = typeof STATUS[keyof typeof STATUS];
Single source of truth — the type is derived from the value.
4. Template literal types for string patterns
type EventName = `on${Capitalize<string>}`;
Useful for constraining event handler names, CSS property prefixes, and similar conventions.
5. infer for extracting inner types
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
type Resolved = UnwrapPromise<Promise<string>>; // string
Comes up whenever you need to peel away a wrapper type generically.