首页 > 解决方案 > Vuetify 2 类型错误:找不到名称“DefaultProps”

问题描述

由于我更新了我的项目以使用 Vuetify 的新 2.x 版本(https://vuetifyjs.com),我在编译过程中遇到了一些类型错误,我不知道如何摆脱它们。正确地只是我的 tsconfig 以某种方式关闭。

我检查了文档并确保在我的 tsconfig.json 的类型部分中包含 vuetify,如下所示:

{
  "compilerOptions": {
    ...

    "types": [
      "webpack-env",
      "jest",
      "vuetify",
      "axios"
      ],

     ...
  }
}

我在这里没有做任何花哨的事情:

import Vue from 'vue';
import App from './App.vue';
import vuetify from './plugins/vuetify';
import router from './router';
import store from './store';

Vue.config.productionTip = false;

new Vue({
  vuetify,
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');

然后我运行开发服务器:yarn serve

ERROR in /Users/sebe/workspace/app/frontend/arena/src/main.ts
12:3 Argument of type '{ vuetify: Vuetify; router: VueRouter; store: Store<any>; render: (h: CreateEle
ment) => VNode; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, Def
aultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.
  Object literal may only specify known properties, and 'vuetify' does not exist in type 'ComponentOpt
ions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>
, Record<string, any>>'.
     8 | 
     9 | new Vue({
  > 10 |   vuetify,
       |   ^
    11 |   router,
    12 |   store,
    13 |   render: (h) => h(App),

ERROR in /Users/sebe/workspace/app/node_modules/vuetify/types/index.d.ts
59:10 Cannot find name 'DefaultData'.
    57 |   export interface ComponentOptions<
    58 |     V extends Vue,
  > 59 |     Data=DefaultData<V>,
       |          ^
    60 |     Methods=DefaultMethods<V>,
    61 |     Computed=DefaultComputed,
    62 |     PropsDef=PropsDefinition<DefaultProps>,

DefaultProps、PropsDefinition、DefaultComputed、DefaultMethods 重复第二个错误。

任何人的帮助都会很棒:)

更新:我刚刚注意到我在使用默认的 vuetify 打字稿模板时遇到了同样的错误:

vue create newapp
vue add vuetify
yarn serve

我的./plugins/vuetify.ts样子是这样的:

import Vue from 'vue';
import Vuetify from 'vuetify/lib';
import colors from 'vuetify/lib/util/colors';
import { VuetifyPreset } from 'vuetify/types/presets';

Vue.use(Vuetify);

const opts: Partial<VuetifyPreset> = {
  theme: {
    dark: true,
    themes: {
      light: {
        primary: colors.green.base,
      },
      dark: {
        primary: colors.green.darken2,
      },
    },
  },
  icons: {
    iconfont: 'mdi',
  },
};

export default new Vuetify(opts);

标签: typescriptvue.jsvuetify.jsvue-clits-loader

解决方案


在 Vuetify 源代码中:

export default Vuetify
export interface Vuetify

模块和接口共享相同的名称 Vuetify 导致此问题。

当 Typescript 想要获取 Vuetify 的接口时,总是 Vuetify 的模块。

要解决这个问题:

import { Vuetify } from 'vuetify'

将此代码添加到您的 main.ts 文件中


推荐阅读