/commands/typescript/tsc-init
tsc --init: create a tsconfig.json file
- 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
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
| Option | Value in the template | Effect |
|---|---|---|
| module | nodenext | Emit ES modules or CommonJS following Node.js rules (the package.json type field and file extensions) |
| target | esnext | Keep modern syntax; nothing is down-levelled |
| types | [] | Load no @types packages automatically; add "node" for Node.js projects |
| sourceMap / declaration / declarationMap | true | Emit .js.map, .d.ts and .d.ts.map next to the JavaScript |
| strict | true | Enable the whole strict family (strictNullChecks, noImplicitAny, ...) |
| noUncheckedIndexedAccess | true | arr[i] and obj[key] include undefined in their type |
| verbatimModuleSyntax | true | Type-only imports must use import type; imports are emitted as written |
| skipLibCheck | true | Skip 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
verbatimModuleSyntaxwithmodule: nodenextrejectsexportin a file that Node.js treats as CommonJS (no"type": "module"inpackage.json), with error TS1287. Add"type": "module"or changemodule. - Missing Node.js types. With
types: [], usingprocessorrequirefails withCannot 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.jsonis JSONC: the comments and the trailing comma are valid for tsc and editors, but a strict JSON parser (for exampleJSON.parsein your own script) rejects them. - Old tutorials show
target: es2016,module: commonjsandesModuleInterop: 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.