typescript
Apply TypeScript for type-safe JavaScript: type design, generics, utility types, strict mode, and integration with modern tooling. Use for any JS codebase.
TypeScript
Type-safe JavaScript with strong typing, generics, and strict tooling.
Quick Start
npm create vite@latest my-app -- --template vanilla-ts
cd my-app
npm i
npm run build
# tsconfig.json enables strict modeWhen to Use
- Any JavaScript project that grows beyond a few files
- Libraries and shared packages where API contracts matter
- Teams that want compile-time safety before runtime
- Refactoring large legacy JS codebases
Best Practices
Type Design
- Model the domain with unions, discriminated unions, and literals
- Derive types from data, not the other way around
- Prefer `type` aliases over `interface` for unions and mapped types
- Export shared types from a central `types.ts`
Strictness
- Enable `strict: true` from the start
- Avoid `any`; use `unknown` and narrow with type guards
- Use `satisfies` to validate objects against a type without widening
- Enable `noUncheckedIndexedAccess` for array safety
Generics
- Write generic functions with sensible constraints
- Use utility types: `Pick`, `Omit`, `Partial`, `Record`, `ReturnType`
- Prefer `infer` in conditional types for advanced extraction
Dependencies
npm i -D typescript @types/node tsx
npx tsc --init
# set: strict true, noUncheckedIndexedAccess trueExamples
// Discriminated union models a state machine safely
type Result<T> =
| { status: "success"; data: T }
| { status: "error"; message: string };
function handle(r: Result<number>): number {
if (r.status === "success") return r.data;
console.error(r.message);
return 0;
}// Generic function with constraint
function pluck<T, K extends keyof T>(items: T[], key: K): T[K][] {
return items.map((item) => item[key]);
}
const names = pluck([{ name: "a", id: 1 }], "name"); // string[]// satisfies keeps narrow types while checking shape
const config = {
port: 3000,
host: "0.0.0.0",
} satisfies Record<string, string | number>;
config.port; // number, not string | number// Type guard narrows unknown
function isError(x: unknown): x is { message: string } {
return typeof x === "object" && x !== null && "message" in x;
}Step-by-Step
1. Enable `strict: true` and `noUncheckedIndexedAccess` in `tsconfig.json`.
2. Model domain types first (unions, entities) before writing functions.
3. Write functions with explicit return types; avoid implicit `any`.
4. Use `unknown` for external input; narrow with guards and validation.
5. Prefer `satisfies` for config objects; use `as const` for literals.
6. Extract complex logic into generics only when reused twice or more.
7. Add `tsc --noEmit` to CI; enforce on every PR.
8. Run `tsc` in watch mode during development for fast feedback.
Validation
1. `tsc --noEmit` passes with zero errors
2. No `any` in new code (lint rule `@typescript-eslint/no-explicit-any`)
3. Public APIs of shared modules have explicit types
4. Runtime behavior matches types (no unsafe casts to work around errors)
5. Build (`tsup`/`vite build`) completes cleanly
Troubleshooting
- "Property does not exist": the type is narrower than you think — narrow with guards.
- "Not assignable": mismatch between declared and actual shape — fix the data contract, not the cast.
- Weird `any` leaks: check `ts-ignore` and third-party typings.
TypeScript (ТайпСкрипт)
Type-safe JavaScript с сильной типизацией, дженериками и строгим тулингом.
Быстрый старт
npm create vite@latest my-app -- --template vanilla-ts
cd my-app
npm i
npm run build
# tsconfig.json включает strict modeКогда использовать
- Любой JavaScript-проект, который вырастает за пару файлов
- Библиотеки и общие пакеты, где важен контракт API
- Команды, которым нужна безопасность на этапе компиляции
- Рефакторинг больших легаси JS-кодовых баз
Лучшие практики
Дизайн типов
- Моделируйте домен через union, discriminated union и литералы
- Выводите типы из данных, а не наоборот
- Предпочитайте `type` вместо `interface` для union и mapped types
- Экспортируйте общие типы из центрального `types.ts`
Строгость
- Включайте `strict: true` с самого начала
- Избегайте `any`; используйте `unknown` и сужайте через type guards
- Используйте `satisfies` для проверки объектов без расширения типа
- Включайте `noUncheckedIndexedAccess` для безопасности массивов
Дженерики
- Пишите generic-функции с разумными ограничениями
- Используйте utility types: `Pick`, `Omit`, `Partial`, `Record`, `ReturnType`
- Применяйте `infer` в conditional types для продвинутого извлечения
Зависимости
npm i -D typescript @types/node tsx
npx tsc --init
# задайте: strict true, noUncheckedIndexedAccess trueПримеры
// Discriminated union безопасно моделирует state machine
type Result<T> =
| { status: "success"; data: T }
| { status: "error"; message: string };
function handle(r: Result<number>): number {
if (r.status === "success") return r.data;
console.error(r.message);
return 0;
}// Generic функция с ограничением
function pluck<T, K extends keyof T>(items: T[], key: K): T[K][] {
return items.map((item) => item[key]);
}
const names = pluck([{ name: "a", id: 1 }], "name"); // string[]// satisfies сохраняет узкие типы, проверяя форму
const config = {
port: 3000,
host: "0.0.0.0",
} satisfies Record<string, string | number>;
config.port; // number, а не string | number// Type guard сужает unknown
function isError(x: unknown): x is { message: string } {
return typeof x === "object" && x !== null && "message" in x;
}Пошаговое руководство
1. Включите `strict: true` и `noUncheckedIndexedAccess` в `tsconfig.json`.
2. Сначала смоделируйте доменные типы (union, сущности) до написания функций.
3. Пишите функции с явными возвращаемыми типами; избегайте неявного `any`.
4. Используйте `unknown` для внешнего ввода; сужайте через guards и валидацию.
5. Предпочитайте `satisfies` для конфигов; используйте `as const` для литералов.
6. Выносите сложную логику в дженерики только при повторном использовании дважды и более.
7. Добавьте `tsc --noEmit` в CI; проверяйте на каждом PR.
8. В разработке запускайте `tsc` в watch mode для быстрой обратной связи.
Валидация
1. `tsc --noEmit` проходит без ошибок
2. Нет `any` в новом коде (правило `@typescript-eslint/no-explicit-any`)
3. Публичные API общих модулей имеют явные типы
4. Поведение в рантайме совпадает с типами (без опасных кастов)
5. Сборка (`tsup`/`vite build`) проходит чисто
Устранение неполадок
- "Property does not exist": тип уже, чем кажется — сузьте через guards.
- "Not assignable": несовпадение объявленной и фактической формы — чините контракт данных, а не каст.
- Странные утечки `any`: проверьте `ts-ignore` и типы сторонних библиотек.