What's tsconfig.json? Why is it so important in the TypeScript project?
tsconfig.json
It's TypeScript. The root directory file of the item, which defines the compile options for the item, the list of the files included/excluded and references to other configurations. No such file, TypeScript The compiler (tsc) will not know how to handle you.
.ts
Documentation.
tsconfig.json Editor It is designed to help developers quickly to configure project structures that are in line with industry best practices in a visualized manner.
Core Configuration Options Parsing
1. Compiled target (Target)
target
It's decided which version of JavaScript the TS code was compiled into. If you need to support the old version browser (e.g. IE11), usually set to
ES5
。 For modern items (e. g. Node.js 18+ or modern browsers), use
ES2022
or
ESNext
to obtain better performance and less code conversion.
2. Strict Mode
It's highly recommended.
"strict": true
。 It's actually a group of checking switches, including
strictNullChecks
、
noImplicitAny
Wait. Turning on the strict mode can capture about 90% of the potential running time errors, which is the greatest value of TypeScript.
3. Modular Interoperability (esModuleInterop)
In JavaScript, there are two systems of CommonJS and ES modules. Open
esModuleInterop
You can use more natural versions of old libraries (e. g. React or Express) when importing them.
import React from 'react'
Syntax: Not cumbersome.
import * as React from 'react'
。
The unique advantages of this tool
- The scenery presets: Provides a one-key preset for mainstream frameworks such as Node.js, React, Vue, to avoid newers losing their way in complex options.
- Real time preview: All UI adjustments are immediately reflected in the code preview on the right, which allows you to understand the specific JSON structure that each option generates.
- Zero learning costs: Each option is accompanied by a Chinese description and a corresponding TS official document term.
- Local security: All configuration generation is done in the local browser, without any server uploading, to protect your project structure privacy.
How to use this editor?
1. Select preset: Select a template that best suits your current item from the left sidebar.
2. Fine-tuning: Tick the switch or select the drop-down menu in the middle area according to your specific needs.
3. Get Configuration: Click on the Copy or Download button on the right to put the generated JSON content in the root directory of the project.
ModuuleResolution Details: rounder and NodeNext
After TypeScript 5.0,moduleResolution Options from the original node/node16/bundler Gradualization. For front-end projects (Vite, Webpack, esbuild etc.) recommended bundlerThe first one was a new one: it assumed that all modules would be packaged. So no need for a graphic. .js suffix, which also allows ESM to mix with CJS in the same project, which is closest to the semantics of modern construction tools.
But the backend project (Node.js run directly) .js) Should be used NodeNext or Node16: It forces the ESM analysis of Node,import The statement must be accompanied by a file suffix.package.json It's... type Fields also need to be set correctly. Node presets for this editor are default configured to NodeNext, avoids running-time errors due to inconsistencies between the packaging and the syntax of the running time.
Include Collaboration with exclude: Avoid misauthorization of third-party dependence
include Specify the range of source files to compile, default is ["src"], which corresponds to the directory structure of most projects. If your source code is app/ or lib/ , manually re-routing.
exclude Default ignores node_modules、dist、build , and the output table does not require additional declaration.
Common trap: when you quote the monorepo bro bag @myorg/uiThe blogger adds: TypeScript may try to recompile it, leading to slower construction and even to conflict of types. Solutions: in exclude Inserts the path to the package in a visible manner or provides the package separately .d.ts Declares the file, which allows tsc to read the type directly without re-compile.
Sub-means after start mode
"strict": true The equivalent is open simultaneously in a set of strict switches, including:
strictNullChecks(Standing strictly) null/undefined)、
noImplicitAny(No hidden any)
strictFunctionTypes(Function parameters are modified from double variants),
strictBindCallApply(bind/call strict type),
strictPropertyInitialization(classic properties must be initialized),
noImplicitThis、useUnknownInCatchVariables、
alwaysStrict。
This editor is default on strict、noImplicitAny、strictNullChecks, These three are 90% of the hidden capture points with the wrong run-time, and it is recommended that they be opened immediately when new items are created. Other subitems can be manually completed in the generated JSON.
Common configuration traps and lines of thought
-
JSX Error reporting: Mostly.
jsx- Picked it, Legacy.reactYes, I do.import React from 'react'。 Changereact-jsx(React 17+) can omit the effect. -
Import
.jsonWrong.: Should open"resolveJsonModule": true, this editor is defaulted to be active. -
"Cannot use import statement outside a module": Inspection
moduleandpackage.jsonIt's...typeis there a consistency of fields. ESM projects should be set up"type": "module"and useNodeNextorESNext。 -
Import Times "esModuleInterop" warning from the CJS library: Open
"esModuleInterop": true, this editor is defaulted to be active. -
Missing after built: Check if it's open
"declaration": true。 The NPM library development preset has been automatically opened.
Compatibility with the official tsconfig base
TypeScript, officially maintained. tsconfig/bases
The warehouse provided a set of pre-encumberable provisions (e.g., @tsconfig/strictest、@tsconfig/vite-react、@tsconfig/create-react-app)。 The configuration generated by this editor overlaps with these presets on the option take value, but aims at "single file is available for use" No dependent npm install Additional packages to facilitate rapid validation and screening by beginners. When the complexity of the project increases, it is smooth to move to extends Mode: Jean tsconfig.json Only the difference items are declared and the base part succeeds the official package.