TypeScript Reference ยท The tsc Command Line
tsc --init
Reference for tsc --init: what the generated tsconfig.json contains, what happens if one already exists, and how to adapt the starter file to a real project.
tsc --init writes a starter tsconfig.json in the current folder. It is the quickest way to turn a folder of .ts files into a project, because from then on plain tsc (with no file names) knows which options to use.
Recent versions generate a short file with sensible modern defaults rather than a long list of commented-out options. You are expected to read it once, adjust it, and commit it. The file is JSON with comments and trailing commas allowed (JSONC), so you can annotate it freely.
tsc --init
Try it
The generated file turns on a strict, modern setup. This site compiles with the same spirit: --strict, a recent target and nodenext modules. The examples below show two things strict mode protects you from.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
tsc --init
A typical starter file looks like this (the exact values depend on the TypeScript version):
{ "compilerOptions": { "module": "nodenext", "target": "esnext", "types": [], "sourceMap": true, "declaration": true, "declarationMap": true, "strict": true, "noUncheckedIndexedAccess": true, "exactOptionalPropertyTypes": true, "verbatimModuleSyntax": true, "isolatedModules": true, "skipLibCheck": true } }
Edge cases:
- If a
tsconfig.jsonalready exists,tsc --initrefuses and prints error TS5054. It never overwrites. - It writes into the current working directory; it does not search upward.
Usage notes
- Treat the output as a draft. Add
outDir,rootDir,includeandlibto match your layout, and remove options you do not understand. types: []is deliberate. Automatic inclusion of every@types/*package is off; list the ones you need, such as"types": ["node"].- Pick
modulefor where code runs.nodenextfor Node projects;esnextwithmoduleResolution: bundlerwhen a bundler does the work. - Without
--init, you can write the file by hand. It only needs{}to exist; defaults then apply. - Check your result with
tsc --showConfig.
Related
- tsc
- tsc --showConfig
- strict
- Tutorial: tsconfig