找不到模块“XXX”或其相应的类型声明。ts(2307)

在基于vue3+vite+typescript的UI组件库时设置了别名会有typescript提示找不到模块“XXX”或其相应的类型声明ts(2307)

别名设置地方 vite.config.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
root: './',
server: {
port: 3001,
},
resolve: {
// 别名
alias: {
'@': resolve(__dirname, 'src'),
'sty': resolve(__dirname, 'styles'),
'pkg': resolve(__dirname, 'packages')
}
}
})

解决方式

tsconfig.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"compilerOptions": {
.........
// 增加
"paths": {
"pkg": ["./packages"],
"@": ["./src"],
}
},
// 增加
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "pkg/**/*.ts", "pkg/**/*.d.ts", "pkg/**/*.tsx", "pkg/**/*.vue"]
}

`