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.tsfiles, 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¶
- 8
TS-1Keepstricton in every project, and don't turn off the checks it includes. It is the default since TypeScript 6.0. TypeScript highly recommends it for libraries, because their declaration files can otherwise fail to compile in projects that use it. (TSConfig, strict, TypeScript 7.0, new behaviors from 6.0, Choosing compiler options: I'm writing a library) - 7
TS-2When another tool compiles your TypeScript, turn onisolatedModules, adderasableSyntaxOnlyif that tool only strips types, and type-check withtsc --noEmitin CI. Bundlers and transpilers such as Vite and esbuild compile one file at a time without type-checking, and Node.js type stripping runs only syntax it can erase, which excludes enums and parameter properties. (TSConfig, isolatedModules, erasableSyntaxOnly, Vite, Transpile only, esbuild, TypeScript caveats, Node.js, Type stripping) - 6
TS-3In new projects, also turn onnoUncheckedIndexedAccessandexactOptionalPropertyTypes.strictleaves them off. The first addsundefinedto values read through an index signature, and the second stops code from assigningundefinedto an optional property that doesn't allow it. Since TypeScript 5.9,tsc --initturns both on. (TSConfig, noUncheckedIndexedAccess, exactOptionalPropertyTypes, TypeScript 5.9, Minimal and updated tsc --init) - 6
TS-4Remove the options that TypeScript 6.0 deprecated, such asbaseUrl,moduleResolution: node10,target: es5and AMD or UMD output, instead of silencing the warnings withignoreDeprecations. TypeScript 7.0 rejects them, and compiling cleanly on 6.0 first makes the move to 7.0 easier. (TypeScript 6.0, Breaking changes and deprecations, TypeScript 7.0, new behaviors from 6.0)
Modules¶
- 7
TS-5Setmodulefor the host that loads your code:nodenextwhen Node.js runs it, andesnextorpreservewithmoduleResolution: bundlerwhen 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-6Turn onverbatimModuleSyntax, and import and export types withimport typeandexport type. The emitted JavaScript then keeps exactly the imports you wrote without atypemodifier, 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-7Don't useany. Useunknownfor values whose type you don't know, and narrow them with type guards before you use them.anyturns off type checking, and every property you read from it isanytoo, 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-8Type data from outside the program, such asJSON.parseresults,fetchresponses, storage and messages, asunknown, and check it at runtime before you narrow it to your own types. Don't claim its shape with anasassertion. Types and type assertions are erased at compile time, so they check nothing at runtime. For server input,NODE-3asks for a schema library. (TypeScript, Erased types, Type assertions, Using type predicates) - 6
TS-9Model a value that can be in one of several states as a discriminated union, and makeswitchstatements over it exhaustive with anevercheck. When you add a state, the compiler then reports every place that doesn't handle it. (TypeScript, Discriminated unions, Exhaustiveness checking)
Libraries¶
- 6
TS-10Check a library with the settings its consumers might use:moduleset tonode18ornodenextrather thanmoduleResolution: bundler,declarationon, and the lowesttargetyou support. Code that resolves underbundlercan fail in Node.js, while code that works in Node.js almost always works in bundlers. If you bundle the library, bundle its declaration files too. (Choosing compiler options: I'm writing a library) - 6
TS-11Publish declaration files with the package and point to them fromexports: put thetypescondition first in each entry, and give the ES module and CommonJS entry points separate declaration files. (TypeScript, Publishing, TypeScript 4.7, package.json exports, Modules reference, package.json "exports")
Large codebases¶
- 6
TS-12Give each environment, such as browser, server, worker and tests, its owntsconfig.json, and connect them, and the packages of a monorepo, with project references built bytsc --build. Onetsconfig.jsoncan describe only one set of globals and module behavior; references also speed up builds and enforce the logical separation between projects. (Choosing compiler options: I'm writing an app, Project References, TypeScript wiki, Performance) - 4
TS-13Turn onisolatedDeclarationsonly when declaration emit limits your build, as in large monorepos, or when another tool generates your declaration files. It requires explicit types on everything a module exports, and TypeScript advises adopting it case by case. (TypeScript 5.5, Isolated declarations, TypeScript 7.0, Project reference builder parallelization)
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 --noEmitin CI, ortsc --buildwith project references. Once the options are set intsconfig.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. ChecksTS-4, and partlyTS-1,TS-2,TS-3andTS-6. - 7 typescript-eslint
no-explicit-anyand theno-unsafe-*rules, such asno-unsafe-assignmentandno-unsafe-member-access: flag explicitany, and values of typeany, such as an uncheckedJSON.parseresult, that are assigned, called, returned or passed on. The first is inrecommended, the others inrecommended-type-checked. The opt-inno-unsafe-type-assertionflagsasassertions that narrow a type. typescript-eslint supports TypeScript up to 6.0, so with TypeScript 7 install TypeScript 6 side by side. ChecksTS-7, and partlyTS-8. - 7 typescript-eslint
switch-exhaustiveness-check: a type-aware rule, not in the shared configs, that reports aswitchover a union of literal types or an enum that misses a case and has nodefault. ChecksTS-9. - 6 arethetypeswrong CLI:
attw --pack .packs the library and checks how its types resolve undernode10,node16andbundlerresolution, reporting problems such as missing types, types that masquerade as ES modules or CommonJS, and wrong default exports. Choose a--profile, such asnode16oresm-only, for the environments you support. ChecksTS-11, and partlyTS-10. - 6 publint rules: lints
package.jsonagainst the files you publish, for exampleEXPORTS_TYPES_SHOULD_BE_FIRSTandEXPORTS_TYPES_INVALID_FORMAT. The CLI's--strictoption reports warnings as errors. Partly checksTS-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.