Reputation: 8052
I'm using Vite to create a new React + TypeScript project.
After creating the project, there are two TypeScript config files on the root folder: tsconfig.json
and tsconfig.node.json
. These are the contents of each one:
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
{
"compilerOptions": {
"composite": true,
"module": "esnext",
"moduleResolution": "node"
},
"include": ["vite.config.ts"]
}
Why do we need two?
What does the second one do?
Can I remove the second one?
Upvotes: 142
Views: 66607
Reputation: 79
Adding on top of the already provided answers:
I did some digging and even checked the source code. The vite.config.ts
seems to be transpiled and loaded directly using typescript API on each build/dev server restart so the reference to it from the "Project tsconfig" did not make much sense to me and then I found the following:
@types/node
to a browser based environment) - Vite GitHub issue: tsconfig.node.json should not be included in tsconfig.json references
project references
or other libraries relying on specific typescript setupUpvotes: 6
Reputation: 37803
You need two different TS configs because the project is using two different environments in which the TypeScript code is executed:
src
folder) is targeting (will be running) inside the browserThus there are two separate configs for those environments and two sets of source files that are defined by the include
and exclude
sections of these configs. Then there is one "master" config tsconfig.json
which "rules them all"
And no, you probably don't want to delete the tsconfig.node.json
but you can probably rename it to something like tsconfig.vite.json
to make its purpose more clear. If you do, make sure to update "main" config accordingly
Upvotes: 115
Reputation: 171
As mentioned by Michal Levý, these are different configs for different environments.
You will notice that tsconfig.json
includes a "references" key which points to an array that includes the tsconfig.node.json
file. If you wish to change the filename of your vite tsconfig, be sure to update this reference.
Upvotes: 12