解决 Vue3 auto-import 自动导入 TS 无法识别

13 min read

In order to properly hint types for auto-imported APIs

Enable options.dts so that auto-imports.d.ts file is automatically generated
Make sure auto-imports.d.ts is not excluded in tsconfig.json

AutoImport({
  dts: true // or a custom path
})

假如生成的 d.ts在项目的根目录下 , 参考如下 tsconfig.json 文件

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["ESNext", "DOM"],
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue","auto-imports.d.ts"],
  "references": [{ "path": "./tsconfig.node.json" }]
}