打字不起作用(TypeScript)

时间:2018-03-30 08:08:41

标签: javascript typescript typescript-typings accounting.js

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

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
}

我该如何解决?

1 个答案:

答案 0 :(得分:2)

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

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