我是 JS 世界的新手。请告诉我正确的方法以及哪里出错了。非常感谢!
我的项目使用
Vite^4.3.2
、React^18.2.0
和 Typescript^5.0.4
。我必须使用带有 pnpm v7.14.0
和 node v16.20.0
的特定外部反应组件库(项目规范要求如此)。
问题:Typescript 和 vscode 的智能感知无法从该库中检测到组件的 props 类型:,所以我不知道该组件有哪些 props 以及每个 props 的类型。智能感知与其他库(如
formik
、dayjs
、react-router-dom
)按预期工作,它显示正确的类型。
没有 ESlint 错误。我检查了
pnpm dev
或构建生产然后预览它,我的应用程序功能在两个环境上都运行良好。
我的
tsconfig.json
文件和Vite默认创建的基本一样:
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"module": "ESNext",
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"]
},
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["./src"],
"exclude": ["node_modules"],
"references": [{ "path": "./tsconfig.node.json" }]
}
tsconfig.node.json
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
vite.config.ts
import {defineConfig} from 'vite';
import react from '@vitejs/plugin-react-swc';
// import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
import svgr from 'vite-plugin-svgr';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths(), svgr()],
server: {
open: true,
},
build: {
commonjsOptions: {
ignoreTryCatch: false,
},
},
});
我不知道如何让智能感知能够检测到组件的 props,如下所示:
所以我尝试将
moduleResolution
更改为 node
并将 "allowSyntheticDefaultImports": true
添加到 tsconfig.json
然后重新启动 vscode。但
仅针对此项目。
附加信息:
环境:Windows 11 - 22H2、
nodejs
v16.20.0、pnpm
v7.14.0、VSCode v1.78.2(用户设置)、.
检查您是否使用旧版本的 VSCode,更新您的 VSCode,错误就会消失。