【问题标题】:Module parse failed when using bigint literal in Angular在 Angular 中使用 bigint 文字时模块解析失败
【发布时间】:2019-09-22 08:36:05
【问题描述】:

我正在做一个个人项目来尝试学习 Angular 框架。我目前是它的新手,但我已经设法(我认为)在打字稿中正确包含 javascript bigint 数字类型(通过在 tsconfig.json 文件中定位 esnext 版本),因为这工作正常:

export class Particule {

  public name: String;
  public amount: bigint;
  public cost: bigint;

  constructor(name: String, amount: bigint, cost: bigint)
  {
      this.amount=amount;
      this.name=name;
      this.cost=cost;
  }

  public increment(): void {
      this.amount += BigInt(1); // <<< this line compiles, no problem
  }

  /* ... */

}

但是,如果我将 increment() 方法的内容替换为:this.amount += 1n;,我会收到以下错误:

ERROR in ./src/app/entities/particule.ts 8:24
Module parse failed: Identifier directly after number (8:24)
You may need an appropriate loader to handle this file type.
|     }
|     increment() {
>         this.amount += 1n;
|     }

这不是一个中继“大”问题(看看我在那里做了什么:D),因为一切都适用于 BigInt() 调用,但我想知道是否有办法在我的项目中直接使用 bigint 文字!

感谢您的回答。

【问题讨论】:

  • Babel/TypeScript 应该更新其编译器以获取该信息帐户。将其作为错误报告给 Angular 团队,Théby 可能会处理它...
  • 实际上,@Olivier,您使用的是哪个版本的typescript

标签: javascript angular typescript bigint


【解决方案1】:

这是因为 BigInt 还不是 ECMAScript 标准的一部分,它是 stage 3 proposal

因此,TypeScript 支持目前是 restricted 以定位 esnext

如果您的目标是 esnext,BigInt 文字应该可以工作。如果不是,错误消息将是:

当定位低于 ESNext 时,BigInt 文字不可用。

也许正在使用旧版本的typescript

【讨论】:

  • tsc -v --&gt; Version 3.4.5 !奇怪的是,对BigInt() 的调用似乎工作正常,但文字却不行!
【解决方案2】:

在上一个答案的基础上,需要对 Angular 项目进行一些更改:

在 package.json "typescript": "4.3.5" 或更高版本

在 tsconfig.json 更新中

"module": "esnext", 
"lib": ["ESNext","dom"]
"target": "esnext"

【讨论】:

    猜你喜欢
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2018-03-14
    • 2019-11-07
    • 2020-06-07
    • 2016-03-06
    • 1970-01-01
    相关资源
    最近更新 更多