打字不起作用 (TypeScript)

Typings isn't working (TypeScript)

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

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

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