问题描述

当你"正确"配置好了所有配置文件后(所以是坑,有个小细节),路径依然不能正确工作时

vite.config.ts:

1
2
3
4
5
6
7
8
9
10
11
import { fileURLToPath } from 'url';
import { defineConfig } from 'vite';

export default defineConfig({
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
});

tsconfig.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

{
"compilerOptions": {
"target": "ES2023",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": [
"ES2023",
"DOM",
"DOM.Iterable"
],
"types": [
"vite/client"
],
"baseUrl": "./",
"paths": {
"@/*": [
"src/*"
]
},
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": [
"./src/*"
]
}

解决方法

请检查tsconfig.jsoninclude数组是否包含你正在写的那个文件,如果你只写了"src/*",但你在test目录下写代码,那test目录就享受不到ts的相关配置

1
2
3
4
"include": [
"./src/*",
"./test/*"
]