javascript - 打字不起作用 (TypeScript)

标签 javascript typescript typescript-typings accounting.js

我在我的代码中使用了这个库

Lib

我在这样的项目中添加了它的类型

https://www.npmjs.com/package/@types/accounting

并将其包含在 .ts 文件中,如下所示

import accounting from "accounting";

我在 /node_modules/@types/accounting 下看到输入内容

但是当我尝试运行项目时。我的 webpack 显示了这个

./app/javascript/components/helpers.ts
Module not found: Error: Can't resolve 'accounting' in '/Users/nemesises/Documents/GitHub/falco-web/app/javascript/components'
 @ ./app/javascript/components/helpers.ts 1:0-36
 @ ./app/javascript/components/itinerary/scripts/hotel_results.ts
 @ ./app/javascript/components/step1/step1.ts
 @ ./app/javascript/components/step1/index.js
 @ ./app/javascript/packs/step1.js
 @ multi (webpack)-dev-server/client?http://localhost:3035 ./app/javascript/packs/step1.js

tsconfig.json 文件

   {
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [ "jquery", "accounting" ],
    "lib": ["es6", "dom"],
    "allowSyntheticDefaultImports": true,
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "**/*.spec.ts",
    "node_modules",
    "vendor",
    "public"
  ],
  "compileOnSave": false
}

我该如何解决这个问题?

最佳答案

使用 TypeScript 时,您必须安装模块、模块本身的类型。最好像这样,以便将类型添加到 devDependency 下,因为它们在运行时不使用。添加实际模块以在运行时使用它,添加类型以便 TypeScript 可以正确检查它。

npm install accounting
npm install --save-dev @types/accounting

关于javascript - 打字不起作用 (TypeScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49570782/

相关文章:

javascript - top.document 是跨浏览器的吗?

typescript - 如何使用不完整的类型

typescript - 如何为 Typescript 使用 socket.IO 客户端类型定义?

Google Chrome 的 Javascript 重定向问题

javascript - jQuery - TableSorter 不工作

angular - 如何扩展 angular2 DatePipe

angular - typescript 中枚举声明中的方括号的含义是什么?

AngularJS ng-model 使用 Controller As 和 Typescript 来填充选择

TypeScript:从其他字符串文字联合类型限制区分联合中的鉴别器

javascript - 将 'active' 类添加到与当前单击的侧边栏菜单链接相同的 href