Skip to content

Commit

Permalink
tsconfig: Use "solution" tsconfig for more correct type checking
Browse files Browse the repository at this point in the history
The change in 2aa6462 is resulting in a vite.config.js and
vite.config.d.ts being generated which we don't want. The
work-around. To fix this, lets do the "solution" tsconfig
approach as used by the create-vue project. This results in
things like the following, all of which aren't enforced
currently:

* tsc allows "process.cwd()" in vite.config.ts and sum.test.ts
  but not in any src files such as app.ts
* tsc allows document.createElement() in src files such as app.ts
  and in sum.test.ts (because jsdom exists there) but not in
  vite.config.ts which is just pure node environment.
* Can't import test files into src files
* Allows using new features such as "".replaceAll() in vite.config.ts
  which should be allowed because we're using node 20.

See:
* vitejs/vite#15913
* https://github.com/vuejs/create-vue/blob/12bf2889b9ca981bcfed894a7c24fa9db5e7bad5/template/tsconfig/base/tsconfig.app.json
* https://github.com/vuejs/create-vue/blob/12bf2889b9ca981bcfed894a7c24fa9db5e7bad5/template/tsconfig/base/tsconfig.node.json
* https://github.com/vuejs/create-vue/blob/12bf2889b9ca981bcfed894a7c24fa9db5e7bad5/template/tsconfig/vitest/tsconfig.vitest.json
  • Loading branch information
philbates35 committed Feb 24, 2024
1 parent f455d31 commit 2a103ee
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
!resources/
!package.json
!tsconfig.json
!tsconfig.node.json
!tsconfig.*.json
!vite.config.ts
7 changes: 6 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ module.exports = {
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["./tsconfig.json", "./tsconfig.node.json"],
project: [
"./tsconfig.json",
"./tsconfig.app.json",
"./tsconfig.node.json",
"./tsconfig.vitest.json",
],
tsconfigRootDir: __dirname,
},
plugins: ["import"],
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Homestead.yaml
auth.json
npm-debug.log
yarn-error.log
*.tsbuildinfo
/.fleet
/.idea
/.vscode
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
!.eslintrc.cjs
!package.json
!tsconfig.json
!tsconfig.node.json
!tsconfig.*.json
!vite.config.ts
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"format-check": "prettier . --check"
},
"devDependencies": {
"@tsconfig/node20": "^20.1.2",
"@types/jsdom": "^21.1.6",
"@types/node": "^20.11.20",
"@vitest/coverage-v8": "^1.2.2",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"include": ["resources/js"],
"exclude": ["resources/js/**/__tests__/*"],
"compilerOptions": {
"composite": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
// Set to empty to avoid accidental inclusion of unwanted types
"types": [],
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Absolute imports */
"paths": {
"@/*": ["./resources/js/*"]
},

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
}
}
37 changes: 11 additions & 26 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Absolute imports */
"paths": {
"@/*": ["./resources/js/*"]
"files": [],
"references": [
{
"path": "./tsconfig.app.json"
},

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["resources/js"],
"references": [{ "path": "./tsconfig.node.json" }]
{
"path": "./tsconfig.node.json"
},
{
"path": "./tsconfig.vitest.json"
}
]
}
14 changes: 8 additions & 6 deletions tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"extends": "@tsconfig/node20/tsconfig.json",
"include": ["vite.config.ts"],
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"noEmit": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",

"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["vite.config.ts"]
"moduleResolution": "Bundler",
"types": ["node"]
}
}
11 changes: 11 additions & 0 deletions tsconfig.vitest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.app.json",
"exclude": [],
"compilerOptions": {
"composite": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.vitest.tsbuildinfo",

"types": ["node", "jsdom"],
"lib": []
}
}

0 comments on commit 2a103ee

Please sign in to comment.