gpt4 book ai didi

javascript - Angular 6 错误 TS8011 类型参数只能在 .ts 文件中使用

转载 作者:行者123 更新时间:2023-11-30 19:18:37 25 4
gpt4 key购买 nike

我有一个 Angular 6 应用程序,我需要包含一个 JS 库。该库是专有的,所以我无法深入了解它的具体内容,但我遇到的问题是 TypeScript 编译器似乎与 <<24>> 这样的字符混淆了。当它真的不是时,认为它是一种类型转换。由于这个问题,我在编译时收到以下错误。

error TS8011: 'type arguments' can only be used in a .ts file

我包含的文件,它正在烘焙,是一个传统的 JS 文件。我无法修改此文件,因为它是一个缩小的库,并且无法通过 NPM 使用。

我最初的想法是让 TS 编译器忽略/排除该文件,但由于某些原因这不起作用。这是我试过但没有用的 tsconfig.json 配置。

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

我试过启用和禁用“allowJs”,但我得到了同样的错误。

你们都知道我怎样才能让 TS 编译器有效地忽略这个文件,这样我就可以用脚本标签导入它吗?

谢谢!

最佳答案

您需要引用这些类型的文件(标准 .js 文件)的方式是以下两种方式之一:

  1. angular.json文件的script数组中添加脚本引用
  2. 直接在index.html文件中添加脚本引用

我会选择第一个选项,因为文档特别说明了这一点:

https://angular.io/guide/workspace-config#additional-build-and-test-options

An object containing JavaScript script files to add to the global context of the project. The scripts are loaded exactly as if you had added them in a tag inside index.html. See more in Styles and scripts configuration below.

我认为您遇到的问题是您正在尝试使用 import 语句或以 tsc(TypeScript 编译器)的方式引用脚本文件正在尝试转换它。

关于javascript - Angular 6 错误 TS8011 类型参数只能在 .ts 文件中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57729977/

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