TypeScript Ecosystem
Core Concepts
- Strict mode: Enable
strict: truefor maximum type safety - Module resolution:
nodenextfor ESM Node.js,bundlerfor Vite/esbuild/webpack - Type narrowing: Use type guards (
typeof,instanceof,in, custom predicates) - Utility types:
Partial,Required,Pick,Omit,Record,Extract,Exclude,ReturnType,Awaited
Recommended tsconfig
Node.js 22 LTS (ES2023):
{
"compilerOptions": {
"target": "ES2023",
"lib": ["ES2023"],
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
Key Strict Options
strictNullChecks- null/undefined handled explicitlynoImplicitAny- Error on implicit anynoUncheckedIndexedAccess- Add undefined to index signaturesnoUnusedLocals/Parameters- Error on unused variables
Type Patterns
Conditional Types with infer
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
type ArrayElement<T> = T extends (infer E)[] ? E : never;
Mapped Types with Key Remapping
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
Custom Type Guards
function isCat(pet: Cat | Dog): pet is Cat {
return (pet as Cat).meow !== undefined;
}
Branded Types
type UserId = string & { readonly __brand: unique symbol };
type OrderId = string & { readonly __brand: unique symbol };
satisfies Operator
const config = {
endpoint: "/api",
timeout: 3000,
} satisfies Record<string, string | number>;
// config.endpoint inferred as "/api" (literal), not string
Anti-Patterns
- any abuse: Use
unknownand narrow with type guards - Type assertions: Use type guards instead of excessive
ascasts - Enums: Use
as constobjects instead - Namespaces: Use ES modules
- Barrel overuse: Can hurt tree-shaking
Tools
tsc --noEmit- Type check onlytsx- Run TypeScript directly with esbuild- ESLint flat config with
typescript-eslint - Vitest or Jest for testing
Context7 Reference
Library ID: /microsoft/typescript
Converted and distributed by TomeVault — claim your Tome and manage your conversions.