首页 > 解决方案 > 无法导入无类型的 npm 模块

问题描述

我添加pathsbaseUrl我的tsconfig.json

{
    "compilerOptions": {
        "outDir": "./built/",
        "sourceMap": true,
        "baseUrl": ".",
        "paths": {
          "*": [ "./typings/*" ]
        },
        "strict": true,
        "noImplicitReturns": true,
        "module": "es2015",
        "moduleResolution": "node",
        "target": "es5"
    },
    "include": [
        "./src/**/*"
    ]
}

我是 Typescript 的新手,我确实看过但无法解决我的问题。

我试着:

--index.ts
import * as VueGoogleMaps from "vue2-google-maps"
Vue.use(VueGoogleMaps, {
 load: {
   key: GMAPS_KEY,
   libraries: 'places',
 }
}

我不断收到错误: TS2345: Argument of type 'typeof 'vue2-google-maps'' is not assignable to parameter of type 'PluginObject<any> | PluginFunction<any>'.

我尝试了一堆东西./typings/index.d.ts

但是我缺少基础知识,我知道VueGoogleMaps应该有一种 PluginObject 类型,但我不知道声明它的语法。

我试过了:

declare module 'vue2-google-maps' {
  import _Vue from 'vue'
  import { PluginObject, PluginFunction } from "vue/types/plugin"
  export default class GMaps<T> implements PluginObject<T> {
    install(Vue: typeof _Vue, options?: T): void
  }
}

任何帮助表示赞赏

标签: typescriptvue.js

解决方案


推荐阅读