gpt4 book ai didi

javascript - 打字不起作用 (TypeScript)

转载 作者:行者123 更新时间:2023-12-01 02:14:14 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com