繁体   English   中英

键入无效(TypeScript)

[英]Typings isn't working (TypeScript)

我在代码中使用了这个库

解放

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

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

并像这样将其包含在.ts文件中

import accounting from "accounting";

我在/node_modules/@types/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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM