Typescript 2 + Angular GoogleMaps TypeScript 定义

4
我创建了一个简单的 Angular 4/Typescript v2.1.6 应用程序。 我试图使用在 https://www.npmjs.com/package/@types/googlemaps 中指定的 googlemaps typescript 定义 (.d.ts 文件)。
我运行了以下命令:
npm install --save @types/googlemaps

然后在我的 app.component.ts 文件中

import * as googlemaps from "googlemaps";

当我执行
npm start

我得到了以下错误
error TS2306: File '/home/dev/project/node_modules/@types/googlemaps/index.d.ts' is not a module.

我的tsconfig.json文件在src文件夹中,而node_modules文件夹与src文件夹处于同一级别,即:

/home/dev/project
  |_src
    |_app/
    |_tsconfig.json
  |_node_modules/
    |_@types/
      |_googlemaps/

tsconfig.json 的内容如下。(我也尝试过不使用 types/typeRoots 属性)
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2015",
      "dom"
    ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": [
      "../node_modules/@types"
    ],
    "types": [
      "googlemaps"
    ]
  }
}

我可以确认文件index.d.ts也存在于位置node_modules/@types/googlemaps/index.d.ts。
我无法确定我缺少什么?
1个回答

0

我安装了一个旧版本的TypeScript,它可以工作!

我之前安装的是TypeScript 2.9,现在我安装了2.6版本,因为新版本验证模块的方式有所不同。或者类似的事情。

npm install typescript@2.6.2

希望能够帮到您。 谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接