简体   繁体   English

为什么 Tsconfig 需要编译 Javascript 文件 Angular

[英]Why Does Tsconfig Need To Compile Javascript Files Angular

I would like to use canvasjs for some charts on an Angular project that i'm making ( https://canvasjs.com/angular-charts/ )我想将 canvasjs 用于我正在制作的 Angular 项目中的一些图表( https://canvasjs.com/angular-charts/

To do so i have downloaded their javascript file and imported it into one of my component as suggested.为此,我下载了他们的 javascript 文件,并按照建议将其导入到我的组件之一中。

import * as CanvasJS from './../../../assets/canvasjs.min.js';

However I sometimes receive when I try to run my application using npm start但是,当我尝试使用 npm start 运行我的应用程序时,有时会收到

ERROR in src/app/Components/multi-camera-view/multi-camera-view.component.ts(2,27): error TS6143: Module './../../../assets/canvasjs.min.js' was resolved to '/src/assets/canvasjs.min.js', but '--allowJs' is not set.

After a quick bit of reading, allowJs allows the ts compiler to compile javascript files, but why the heck does it need to compile those if they're already in javascript?快速阅读后,allowJs 允许 ts 编译器编译 javascript 文件,但是如果它们已经在 javascript 中,为什么还需要编译它们呢?

Browsers can't read typescript but they can read javascript.浏览器无法读取打字稿,但可以读取 javascript。 So why does it need to compile it?那么为什么需要编译它呢?

I tried adding the allowJs flag in my tsconfig.json file at the root我尝试在根目录的 tsconfig.json 文件中添加 allowJs 标志

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "allowJs": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

But then i just got the error:但后来我得到了错误:

ERROR in error TS5055: Cannot write file '/src/assets/canvasjs.min.js' because it would overwrite input file.
  Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.

I visited the link and set noEmit to true, but then my application would build fine but wouldn't run.我访问了该链接并将 noEmit 设置为 true,但随后我的应用程序可以正常构建但无法运行。 So, what's the best way to use this javascript file?那么,使用这个 javascript 文件的最佳方式是什么?

Thanks!谢谢!

The --allowJS flag allows the compiler to infer the type information from the JavaScript file so it can type-check your use of that file. --allowJS标志允许编译器从 JavaScript 文件推断类型信息,以便它可以对您对该文件的使用进行类型检查。

It may not be as rich as the type information from a TypeScript file, but it does a pretty good job of working out type information in most cases.它可能不如 TypeScript 文件中的类型信息丰富,但在大多数情况下,它在计算类型信息方面做得非常好。

So it's not to convert JavaScript into yet more JavaScript, but to make your TypeScript code that depends on JavaScript easier to manage.所以不是将 JavaScript 转换成更多的 JavaScript,而是让你依赖 JavaScript 的 TypeScript 代码更容易管理。

I spent a lot of time on this.我花了很多时间在这上面。

So I had allowJs set in my tsconfig.json at the root.所以我在根目录的 tsconfig.json 中设置了 allowJs。 This resulted in the compile error.这导致了编译错误。 Despite setting an outDir, I still had issues with overwriting.尽管设置了 outDir,但我仍然遇到覆盖问题。

Turns out the tsconfig.json at the root is not the place to set allowJs.结果发现根目录下的 tsconfig.json 不是设置 allowJs 的地方。

In my src/tsconfig.app.json I set allowJs to be true and it then worked successfully.在我的 src/tsconfig.app.json 中,我将 allowJs 设置为 true,然后它就成功运行了。

If you get Error while using canvas.js in angular如果在 angular 中使用canvas.js 时出现错误

In tsconfig.app.json File在 tsconfig.app.json 文件中

add添加

"allowJs":true;

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

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