As others have mentioned, the existing answer does not solve the problem if the frontend and backend have different types - which is in nearly every case, as frontend code supports the DOM (and not the node.js standard library) whereas backend code supports the node.js standard library (and generally not the DOM).
Having a top level tsconfig.json would mean that dom code would show up as errors in frontend code (if dom is a lib) or that dom code would be allowed in backend code (if dom is omitted).
Here's a working solution:
Folder Structure
Our projects tend to be 'backend by default' with a specific folder for frontend code.
├── src/
│ ├── frontend/ < Frontend
│ │ ├── `tsconfig.json` (extends frontend framework defaults, eg Svelte)
│ ├── http/ < Backend
│ ├── events/ < Backend
├── tsconfig.json `tsconfig.json` (backend tsconfig)
Backend tsconfig.json
This is usually fairly minimal. We use jest for testing and the es2019 JS stdlib.
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"outDir": "dist",
"moduleResolution": "node",
"esModuleInterop": true,
"lib": ["es2019"],
"types": ["jest"],
},
"exclude": [
"node_modules",
"public/*",
"src/frontend/*"
],
"include": ["src/**/*"]
}
Frontend tsconfig.json
This is for Svelte but would work similarly in older frameworks. The frontend has different types because it supports .svelte files and the dom
{
"extends": "@tsconfig/svelte/tsconfig.json",
"compilerOptions": {
// Default included above is es2017
"target": "es2019",
},
"lib": ["es2019", "dom"],
}
Frontend specific tools
Making rollup use a separate tsconfig file:
export default {
input: ...
output: ...
plugins: [
...
typescript({
tsconfig: "src/frontend/tsconfig.json",
sourceMap: isDevelopment,
inlineSources: isDevelopment,
}),
...
],
...
};