--target es2022

TL;DR

--target es2022 が指定できるようになった。

サンプルコード

以下のコードを es5es2022 それぞれでトランスパイルした結果を比較する。

class Foo {
  private className = 'FOO';
}

function foo() {
  return [0, 1, 2].at(0);
}

--target es5

tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
  }
}

Array.atはそもそもコンパイルエラーになる。

return [0, 1, 2].at(0);
// ❌ ERROR! Property 'at' does not exist on type 'number[]'.

トランスパイル後のJavaScriptは以下のようになる。

"use strict";
var Foo = /** @class */ (function () {
    function Foo() {
        this.className = 'FOO';
    }
    return Foo;
}());
function foo() {
    return [0, 1, 2].at(0);
}

--target es2022

tsconfig.json
{
  "compilerOptions": {
    "target": "es2022",
  }
}

トランスパイル後のJavaScriptは以下のようになる。

"use strict";
class Foo {
    constructor() {
        this.className = 'FOO';
    }
}
function foo() {
    return [0, 1, 2].at(0);
}

Last updated