Loading...
Loading...
TypeScript best practices and patterns for writing type-safe, maintainable code. Use when working with TypeScript files, configuring tsconfig, defining interfaces/types, implementing error handling, writing generics, or setting up type-safe communication patterns. Includes patterns for discriminated unions, type guards, utility types, and more.
npx skill4agent add ghosttypes/ff-5mp-api-ts typescript-best-practicesimport typetsconfig.json{
"strict": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true
}// Explicit null handling
export function findById<T>(items: Map<string, T>, id: string): T | null {
return items.get(id) ?? null;
}
// Optional chaining and nullish coalescing
export function getName(item?: Item): string {
return item?.name ?? 'Unknown';
}// Good
export function validateConfig(data: unknown): ValidatedConfig | null {
const result = ConfigSchema.safeParse(data);
return result.success ? result.data : null;
}
// Bad - implicit return type
export function validateConfig(data) {
return data;
}export type ConnectionState =
| { status: 'disconnected' }
| { status: 'connecting'; progress: number }
| { status: 'connected'; connectionId: string }
| { status: 'error'; message: string };
// TypeScript knows which properties are available in each branchexport type Result<T, E = Error> =
| { success: true; data: T }
| { success: false; error: E };
const result = await connectToService(options);
if (result.success) {
console.log('Connected:', result.data.id);
} else {
console.error('Failed:', result.error.message);
}readonlyexport interface Config {
readonly enableFeature: boolean;
readonly port: number;
}// Type guard
export function isUserData(data: unknown): data is UserData {
return typeof data === 'object' && data !== null && 'name' in data;
}
// Assertion function
export function assertIsDefined<T>(value: T): asserts value is NonNullable<T> {
if (value === undefined || value === null) throw new Error('Value is undefined or null');
}// Type-only imports (preferred)
import type { Config } from '../types/config';
// Named exports (avoid default exports)
export class DataService {}
export const getService = () => DataService.getInstance();
// Grouped imports
import { External } from 'external'; // External deps
import { internalUtil } from './utils'; // Internal utils
import type { MyType } from './types'; // Types| Reference File | When to Load |
|---|---|
| Setting up TypeScript configuration |
| Defining interfaces, generic types, extending external types |
| Working with discriminated unions, result types, exhaustive checks |
| Runtime validation, branded types, assertion functions |
| Using built-in utilities, const assertions, template literal types |
| Implementing structured errors, error factories, retry logic |
| Building generic services, repositories, factories, event emitters |
| Organizing imports/exports, avoiding circular dependencies |
anyunknown{ a?: { b?: string } }