如何在Vite/unplugin-auto-import中自动导入declarations.d.ts?
问题:无法自动导入
src/declarations.d.ts到Vue组件中 我尝试将src/declarations.d.ts自动导入到Vue组件中,但未能成功。以下是我的vite.config.ts配置:
import { fileURLToPath, URL } from 'url' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import Pages from 'vite-plugin-pages' import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' export default defineConfig({ plugins: [ vue(), Pages(), AutoImport({ // targets to transform include: [ /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx /\.vue$/, /\.vue\?vue/, // .vue /\.md$/, // .md ], // global imports to register imports: [ // presets 'vue', 'vue-router', // custom { 'declarations': [], '@vueuse/core': [ // named imports 'onClickOutside', 'useToggle', 'useDark', 'useElementSize', // import { onClickOutside } from '@vueuse/core', // alias ['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core', ], 'axios': [ // default imports ['default', 'axios'], // import { default as axios } from 'axios', ], '[package-name]': [ '[import-names]', // alias ['[from]', '[alias]'], ], }, ], // Enable auto import by filename for default module exports under directories defaultExportByFilename: false, // Auto import for module exports under directories // by default it only scan one level of modules under the directory dirs: [ './src/lib', // './hooks', // './composables/**', // all nested modules // ... ], // Filepath to generate corresponding .d.ts file. // Defaults to './auto-imports.d.ts' when `typescript` is installed locally. // Set `false` to disable. dts: './auto-imports.d.ts', // Auto import inside Vue template // see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72 vueTemplate: true, // Custom resolvers, compatible with `unplugin-vue-components` // see https://github.com/antfu/unplugin-auto-import/pull/23/ resolvers: [ /* ... */ ], // Generate corresponding .eslintrc-auto-import.json file. // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals eslintrc: { enabled: false, // Default `false` filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json` globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable') }, }), Components({ dirs: ['src', 'node_modules'] }), ], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } }, server: { port: 3000, } })
我尝试将其添加到全局导入中,但没有效果。
补充说明
auto-imports.d.ts内容如下:
/* eslint-disable */ /* prettier-ignore */ // @ts-nocheck // Generated by unplugin-auto-import export {} declare global { const EffectScope: typeof import('vue')['EffectScope'] const [alias]: typeof import('[package-name]')['[from]'] const [import-names]: typeof import('[package-name]')['[import-names]'] const axios: typeof import('axios')['default'] const computed: typeof import('vue')['computed'] const createApp: typeof import('vue')['createApp'] const customRef: typeof import('vue')['customRef'] const defineAsyncComponent: typeof import('vue')['defineAsyncComponent'] const defineComponent: typeof import('vue')['defineComponent'] const effectScope: typeof import('vue')['effectScope'] const getCurrentInstance: typeof import('vue')['getCurrentInstance'] const getCurrentScope: typeof import('vue')['getCurrentScope'] const h: typeof import('vue')['h'] const inject: typeof import('vue')['inject'] const isProxy: typeof import('vue')['isProxy'] const isReactive: typeof import('vue')['isReactive'] const isReadonly: typeof import('vue')['isReadonly'] const isRef: typeof import('vue')['isRef'] const markRaw: typeof import('vue')['markRaw'] const nextTick: typeof import('vue')['nextTick'] const onActivated: typeof import('vue')['onActivated'] const onBeforeMount: typeof import('vue')['onBeforeMount'] const onBeforeRouteLeave: typeof import('vue-router')['onBeforeRouteLeave'] const onBeforeRouteUpdate: typeof import('vue-router')['onBeforeRouteUpdate'] const onBeforeUnmount: typeof import('vue')['onBeforeUnmount'] const onBeforeUpdate: typeof import('vue')['onBeforeUpdate'] const onClickOutside: typeof import('@vueuse/core')['onClickOutside'] const onDeactivated: typeof import('vue')['onDeactivated'] const onErrorCaptured: typeof import('vue')['onErrorCaptured'] const onMounted: typeof import('vue')['onMounted'] const onRenderTracked: typeof import('vue')['onRenderTracked'] const onRenderTriggered: typeof import('vue')['onRenderTriggered'] const onScopeDispose: typeof import('vue')['onScopeDispose'] const onServerPrefetch: typeof import('vue')['onServerPrefetch'] const onUnmounted: typeof import('vue')['onUnmounted'] const onUpdated: typeof import('vue')['onUpdated'] const provide: typeof import('vue')['provide'] const reactive: typeof import('vue')['reactive'] const readonly: typeof import('vue')['readonly'] const ref: typeof import('vue')['ref'] const resolveComponent: typeof import('vue')['resolveComponent'] const router: typeof import('./src/lib/router')['default'] const shallowReactive: typeof import('vue')['shallowReactive'] const shallowReadonly: typeof import('vue')['shallowReadonly'] const shallowRef: typeof import('vue')['shallowRef'] const store: typeof import('./src/lib/store')['store'] const tiptapNodeExtension: typeof import('./src/lib/tiptap-node-extension')['default'] const toRaw: typeof import('vue')['toRaw'] const toRef: typeof import('vue')['toRef'] const toRefs: typeof import('vue')['toRefs'] const triggerRef: typeof import('vue')['triggerRef'] const unref: typeof import('vue')['unref'] const useAttrs: typeof import('vue')['useAttrs'] const useCssModule: typeof import('vue')['useCssModule'] const useCssVars: typeof import('vue')['useCssVars'] const useDark: typeof import('@vueuse/core')['useDark'] const useElementSize: typeof import('@vueuse/core')['useElementSize'] const useLink: typeof import('vue-router')['useLink'] const useMyFetch: typeof import('@vueuse/core')['useFetch'] const useRoute: typeof import('vue-router')['useRoute'] const useRouter: typeof import('vue-router')['useRouter'] const useSlots: typeof import('vue')['useSlots'] const useToggle: typeof import('@vueuse/core')['useToggle'] const watch: typeof import('vue')['watch'] const watchEffect: typeof import('vue')['watchEffect'] const watchPostEffect: typeof import('vue')['watchPostEffect'] const watchSyncEffect: typeof import('vue')['watchSyncEffect'] } // for type re-export declare global { // @ts-ignore export type { Component, ComponentPublicInstance, ComputedRef, InjectionKey, PropType, Ref, VNode } from 'vue' } // for vue template auto import import { UnwrapRef } from 'vue' declare module 'vue' { interface ComponentCustomProperties { readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']> readonly [alias]: UnwrapRef<typeof import('[package-name]')['[from]']> readonly [import-names]: UnwrapRef<typeof import('[package-name]')['[import-names]']> readonly axios: UnwrapRef<typeof import('axios')['default']> readonly computed: UnwrapRef<typeof import('vue')['computed']> readonly createApp: UnwrapRef<typeof import('vue')['createApp']> readonly customRef: UnwrapRef<typeof import('vue')['customRef']> readonly defineAsyncComponent: UnwrapRef<typeof import('vue')['defineAsyncComponent']> readonly defineComponent: UnwrapRef<typeof import('vue')['defineComponent']> readonly effectScope: UnwrapRef<typeof import('vue')['effectScope']> readonly getCurrentInstance: UnwrapRef<typeof import('vue')['getCurrentInstance']> readonly getCurrentScope: UnwrapRef<typeof import('vue')['getCurrentScope']> readonly h: UnwrapRef<typeof import('vue')['h']> readonly inject: UnwrapRef<typeof import('vue')['inject']> readonly isProxy: UnwrapRef<typeof import('vue')['isProxy']> readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']> readonly isReadonly: UnwrapRef<typeof import('vue')['isReadonly']> readonly isRef: UnwrapRef<typeof import('vue')['isRef']> readonly markRaw: UnwrapRef<typeof import('vue')['markRaw']> readonly nextTick: UnwrapRef<typeof import('vue')['nextTick']> readonly onActivated: UnwrapRef<typeof import('vue')['onActivated']> readonly onBeforeMount: UnwrapRef<typeof import('vue')['onBeforeMount']> readonly onBeforeRouteLeave: UnwrapRef<typeof import('vue-router')['onBeforeRouteLeave']> readonly onBeforeRouteUpdate: UnwrapRef<typeof import('vue-router')['onBeforeRouteUpdate']> readonly onBeforeUnmount: UnwrapRef<typeof import('vue')['onBeforeUnmount']> readonly onBeforeUpdate: UnwrapRef<typeof import('vue')['onBeforeUpdate']> readonly onClickOutside: UnwrapRef<typeof import('@vueuse/core')['onClickOutside']> readonly onDeactivated: UnwrapRef<typeof import('vue')['onDeactivated']> readonly onErrorCaptured: UnwrapRef<typeof import('vue')['onErrorCaptured']> readonly onMounted: UnwrapRef<typeof import('vue')['onMounted']> readonly onRenderTracked: UnwrapRef<typeof import('vue')['onRenderTracked']> readonly onRenderTriggered: UnwrapRef<typeof import('vue')['onRenderTriggered']> readonly onScopeDispose: UnwrapRef<typeof import('vue')['onScopeDispose']> readonly onServerPrefetch: UnwrapRef<typeof import('vue')['onServerPrefetch']> readonly onUnmounted: UnwrapRef<typeof import('vue')['onUnmounted']> readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']> readonly provide: UnwrapRef<typeof import('vue')['provide']> readonly reactive: UnwrapRef<typeof import('vue')['reactive']> readonly readonly: UnwrapRef<typeof import('vue')['readonly']> readonly ref: UnwrapRef<typeof import('vue')['ref']> readonly resolveComponent: UnwrapRef<typeof import('vue')['resolveComponent']> readonly router: UnwrapRef<typeof import('./src/lib/router')['default']> readonly shallowReactive: UnwrapRef<typeof import('vue')['shallowReactive']> readonly shallowReadonly: UnwrapRef<typeof import('vue')['shallowReadonly']> readonly shallowRef: UnwrapRef<typeof import('vue')['shallowRef']> readonly store: UnwrapRef<typeof import('./src/lib/store')['store']> readonly tiptapNodeExtension: UnwrapRef<typeof import('./src/lib/tiptap-node-extension')['default']> readonly toRaw: UnwrapRef<typeof import('vue')['toRaw']> readonly toRef: UnwrapRef<typeof import('vue')['toRef']> readonly toRefs: UnwrapRef<typeof import('vue')['toRefs']> readonly triggerRef: UnwrapRef<typeof import('vue')['triggerRef']> readonly unref: UnwrapRef<typeof import('vue')['unref']> readonly useAttrs: UnwrapRef<typeof import('vue')['useAttrs']> readonly useCssModule: UnwrapRef<typeof import('vue')['useCssModule']> readonly useCssVars: UnwrapRef<typeof import('vue')['useCssVars']> readonly useDark: UnwrapRef<typeof import('@vueuse/core')['useDark']> readonly useElementSize: UnwrapRef<typeof import('@vueuse/core')['useElementSize']> readonly useLink: UnwrapRef<typeof import('vue-router')['useLink']> readonly useMyFetch: UnwrapRef<typeof import('@vueuse/core')['useFetch']> readonly useRoute: UnwrapRef<typeof import('vue-router')['useRoute']> readonly useRouter: UnwrapRef<typeof import('vue-router')['useRouter']> readonly useSlots: UnwrapRef<typeof import('vue')['useSlots']> readonly useToggle: UnwrapRef<typeof import('@vueuse/core')['useToggle']> readonly watch: UnwrapRef<typeof import('vue')['watch']> readonly watchEffect: UnwrapRef<typeof import('vue')['watchEffect']> readonly watchPostEffect: UnwrapRef<typeof import('vue')['watchPostEffect']> readonly watchSyncEffect: UnwrapRef<typeof import('vue')['watchSyncEffect']> } }
解决方案
1. 明确类型声明文件的作用
src/declarations.d.ts是TypeScript类型声明文件,仅用于提供类型提示,不包含可在运行时导入的变量或函数。unplugin-auto-import是用来自动导入运行时值的,所以用它处理类型声明文件本身就不对路。
2. 让TypeScript自动识别类型
要让Vue组件自动识别该文件中的类型,只需确保TypeScript能扫描到它:
- 打开项目根目录的
tsconfig.json,检查include数组是否包含src/**/*.d.ts:{ "include": [ "src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue" ] } - 如果
declarations.d.ts里有declare global的全局类型声明,TypeScript会自动全局识别,无需手动导入。
3. 处理导出的类型别名/接口
如果文件里导出了类型(比如export type MyType = { ... }),在组件中使用时可以手动导入,也可以通过tsconfig.json简化:
{ "compilerOptions": { "typeRoots": ["./src/types", "./node_modules/@types"] } }
把declarations.d.ts放到src/types目录下,TypeScript会自动加载该目录下的所有类型声明。
4. 清理无效配置
你的vite.config.ts中AutoImport的imports里有一段示例占位代码:
{ 'declarations': [], '[package-name]': [ '[import-names]', ['[from]', '[alias]'], ], }
这段代码没有实际作用,建议删除,避免干扰配置逻辑。
内容的提问来源于stack exchange,提问作者Artur Müller Romanov
相关产品推荐
相关产品推荐

