Skip to content

/commands/typescript/tsc-init

tsc --init: create a tsconfig.json file

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · v24.20.0updated
tool
TypeScript
versions
TypeScript 5.9+ (short template), TypeScript 7.0
os
any

npx tsc --init writes a tsconfig.json in the current directory with a recommended set of compiler options: strict type checking, modern module/target settings and comments that explain the options you may want to turn on. Since TypeScript 5.9 the template is short (about 40 lines); older versions wrote a very long file with almost every option commented out.

Create the file

bash
npm init -y >/dev/null
npm install --silent --no-audit --no-fund --save-dev typescript@7.0.2 >/dev/null 2>&1
npx tsc --init
grep -v '^ *//' tsconfig.json | grep -v '^ *$'

Output

Created a new tsconfig.json

You can learn more at https://aka.ms/tsconfig
{
  "compilerOptions": {
    "module": "nodenext",
    "target": "esnext",
    "types": [],
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "strict": true,
    "jsx": "react-jsx",
    "verbatimModuleSyntax": true,
    "isolatedModules": true,
    "noUncheckedSideEffectImports": true,
    "moduleDetection": "force",
    "skipLibCheck": true,
  }
}

The grep hides the comment lines so only the active options remain. Without a global install, npx tsc runs the project's own compiler from node_modules/.bin, which keeps the version in package.json.

What the main options mean

OptionValue in the templateEffect
modulenodenextEmit ES modules or CommonJS following Node.js rules (the package.json type field and file extensions)
targetesnextKeep modern syntax; nothing is down-levelled
types[]Load no @types packages automatically; add "node" for Node.js projects
sourceMap / declaration / declarationMaptrueEmit .js.map, .d.ts and .d.ts.map next to the JavaScript
stricttrueEnable the whole strict family (strictNullChecks, noImplicitAny, ...)
noUncheckedIndexedAccesstruearr[i] and obj[key] include undefined in their type
verbatimModuleSyntaxtrueType-only imports must use import type; imports are emitted as written
skipLibChecktrueSkip type checking inside .d.ts files from dependencies

rootDir and outDir are commented out: uncomment them (./src and ./dist) to keep compiled files out of the source tree.

Pass options while generating

Any compiler flag given together with --init is written into the file, for example npx tsc --init --rootDir src --outDir dist --module commonjs. That is quicker than editing the JSON afterwards and avoids typos in option names.

Pitfalls

  • Errors right after init. The template is strict, and verbatimModuleSyntax with module: nodenext rejects export in a file that Node.js treats as CommonJS (no "type": "module" in package.json), with error TS1287. Add "type": "module" or change module.
  • Missing Node.js types. With types: [], using process or require fails with Cannot find name 'process' until you add "types": ["node"] and install @types/node. Module resolution problems show up as TS2307 Cannot find module.
  • Comments and trailing commas. tsconfig.json is JSONC: the comments and the trailing comma are valid for tsc and editors, but a strict JSON parser (for example JSON.parse in your own script) rejects them.
  • Old tutorials show target: es2016, module: commonjs and esModuleInterop: that was the template before TypeScript 5.9.

Confirm it worked

npx tsc --showConfig prints the final configuration that tsc will use, including the files it found. Running npx tsc with no arguments now compiles the project according to the new file.

FAQ

Where does tsc --init put the file?

In the current directory. tsc only finds a tsconfig.json by looking in the current directory and its parents, so run it from the project root.

Can tsc --init overwrite an existing tsconfig.json?

No. If the file already exists, tsc stops with error TS5054 and leaves it unchanged. Delete or rename the old file first if you want a fresh template.

Why does the generated file set types to an empty array?

The template no longer loads every @types package in node_modules automatically. For a Node.js project, set types to ["node"] and install @types/node, as the comment in the generated file says.

Sources