Skip to content

TypeScript

Scope: the TypeScript language and compiler setup in any TypeScript codebase, such as front ends, back ends, libraries and tools, for TypeScript 6.0 and 7.0 (the native compiler). Platform files add their own rules: Node.js covers handling promises (NODE-2), validating server input at runtime (NODE-3) and type-checking services in CI (NODE-8), and React, Next.js, Vue and Svelte cover their frameworks. For versioning published packages, see general/releases.md; for monorepo layout, see general/repositories.md.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

  • 9 TSConfig Reference (TypeScript): the baseline for compiler settings: every compiler option with its default, the options TypeScript marks as recommended, and examples.
  • 9 The TypeScript Handbook (TypeScript): the official guide to the language and its type system: everyday types, narrowing, functions, object types, generics and type manipulation.
  • 8 Modules (TypeScript Handbook): how TypeScript models the module format and module resolution of the runtime or bundler that loads your code, with a guide to choosing compiler options for apps, bundlers, Node.js and libraries.
  • 7 Announcing TypeScript 7.0 (TypeScript team, 2026): the native compiler, the defaults it adopts from TypeScript 6.0, the options it no longer supports, and running it side by side with TypeScript 6.0 for tools that need the compiler API.
  • 7 Project References (TypeScript): splitting a codebase into projects that reference each other and build incrementally with tsc --build.
  • 7 Declaration Files (TypeScript): writing and publishing .d.ts files, including Do's and Don'ts for the types you expose.
  • 6 Performance (TypeScript wiki): writing code that compiles quickly, using project references, configuring tsconfig.json, and investigating slow builds.

Key rules

Compiler settings

Modules

  • 7 TS-5 Set module for the host that loads your code: nodenext when Node.js runs it, and esnext or preserve with moduleResolution: bundler when a bundler does. TypeScript checks imports by modeling that host, so a mismatch hides import errors until runtime. (TypeScript, Modules: Theory, Choosing compiler options, TSConfig, module)
  • 7 TS-6 Turn on verbatimModuleSyntax, and import and export types with import type and export type. The emitted JavaScript then keeps exactly the imports you wrote without a type modifier, so other compilers produce the same output and no import is dropped by surprise. It doesn't work when one source file is compiled to both ES modules and CommonJS. (TSConfig, verbatimModuleSyntax, Choosing compiler options)

Types

  • 7 TS-7 Don't use any. Use unknown for values whose type you don't know, and narrow them with type guards before you use them. any turns off type checking, and every property you read from it is any too, so it spreads. Keep it for code you are migrating from JavaScript. (TypeScript, Do's and Don'ts: any, Everyday Types: any, More on Functions: unknown)
  • 7 TS-8 Type data from outside the program, such as JSON.parse results, fetch responses, storage and messages, as unknown, and check it at runtime before you narrow it to your own types. Don't claim its shape with an as assertion. Types and type assertions are erased at compile time, so they check nothing at runtime. For server input, NODE-3 asks for a schema library. (TypeScript, Erased types, Type assertions, Using type predicates)
  • 6 TS-9 Model a value that can be in one of several states as a discriminated union, and make switch statements over it exhaustive with a never check. When you add a state, the compiler then reports every place that doesn't handle it. (TypeScript, Discriminated unions, Exhaustiveness checking)

Libraries

Large codebases

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 tsc (the TypeScript compiler): run tsc --noEmit in CI, or tsc --build with project references. Once the options are set in tsconfig.json, it fails on code that breaks them, and TypeScript 7.0 fails on options deprecated in 6.0. It can't tell whether an option is missing. Checks TS-4, and partly TS-1, TS-2, TS-3 and TS-6.
  • 7 typescript-eslint no-explicit-any and the no-unsafe-* rules, such as no-unsafe-assignment and no-unsafe-member-access: flag explicit any, and values of type any, such as an unchecked JSON.parse result, that are assigned, called, returned or passed on. The first is in recommended, the others in recommended-type-checked. The opt-in no-unsafe-type-assertion flags as assertions that narrow a type. typescript-eslint supports TypeScript up to 6.0, so with TypeScript 7 install TypeScript 6 side by side. Checks TS-7, and partly TS-8.
  • 7 typescript-eslint switch-exhaustiveness-check: a type-aware rule, not in the shared configs, that reports a switch over a union of literal types or an enum that misses a case and has no default. Checks TS-9.
  • 6 arethetypeswrong CLI: attw --pack . packs the library and checks how its types resolve under node10, node16 and bundler resolution, reporting problems such as missing types, types that masquerade as ES modules or CommonJS, and wrong default exports. Choose a --profile, such as node16 or esm-only, for the environments you support. Checks TS-11, and partly TS-10.
  • 6 publint rules: lints package.json against the files you publish, for example EXPORTS_TYPES_SHOULD_BE_FIRST and EXPORTS_TYPES_INVALID_FORMAT. The CLI's --strict option reports warnings as errors. Partly checks TS-11.

Tutorials and further reading

  • 7 Effective TypeScript (Dan Vanderkam, 2nd edition, 2024): 83 items of specific advice on the type system, type inference, type design, any, generics, declaration files and migration.
  • 6 Total TypeScript Essentials (Matt Pocock): a free online book from setup and essential types to narrowing, modules, configuration and designing your types.