【问题标题】:Operational chaining not working properly in NodeJS TS操作链在 NodeJS TS 中无法正常工作
【发布时间】:2021-05-16 22:47:56
【问题描述】:

我有一个带有 Typescript 的 Node 脚本,我想(从文件中)提取一个对象,该对象偶尔为空。因此,我想使用操作链接(或其他一些运算符),如果对象不存在,我可以回退到 null

     const {
        type: jackpotType,
        contribution: jackpotContribution,
        contributionEnable: jackpotContributionEnable,
      }: {
        type: string | null;
        contribution: number | null;
        contributionEnable: boolean | null;
      } = entry.data?.jackpot;

当我编译我得到这个错误: SyntaxError: Unexpected token .

我已经检查了我的节点版本(最近升级),它应该是兼容的。

$ node --version // v15.8.0

在我的tsconfig.json 中,我有以下设置

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "ESNEXT" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    // "lib": [],              
....

【问题讨论】:

    标签: node.js typescript


    【解决方案1】:

    将您的 target 字段更改为适合 Node v14 的 ES2020

    {
      "compilerOptions": {
        "target": "ES2020",
        "lib": ["ES2020"],
        "moduleResolution": "node",
        "module": "commonjs"      
    ....
    

    基于What TypeScript configuration produces output closest to Node.js 14 capabilities?

    【讨论】:

      猜你喜欢
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      • 2019-01-11
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多