1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

『erasableSyntaxOnly が有効な場合、この構文は使用できません』

Posted at

はじめに

クラス作成していたら、erasableSyntaxOnly が有効な場合、この構文は使用できませんが出ました。

問題

問題コードは以下です。

export class UserClass {
  constructor(
    public name: string,
    public description: string,
  ) { }
}

TypeScripterasableSyntaxOnly(TS 5.8で追加)が有効になっている状態で、「型注釈を消すだけではJavaScriptとして成立しない/同じ動きにならないTypeScript固有構文」を書いたときに出るエラーです。

解決方法

以下の書き方に変えます。

export class UserClass {
  public name: string;
  public description: string;


  constructor(name: string, description: string) {
    this.name = name;
    this.description = description;
  }
}

そのほかに、erasableSyntaxOnlyをオフにすれば解決します。

おわりに

Javaなどの書き方が慣れてる人はコントラクタに処理を書いた方が見慣れています。
ただ、erasableSyntaxOnlyをオンにしてると便利なTypeScriptの構文が使えないです。
erasableSyntaxOnly を オフにしても、Nodeの “型を消すだけ” 実行(type stripping)を前提にしている場合は、結局そのコードは動きません(変換が必要だから)。その場合は ビルド(Vite/tsc等でトランスパイル)してから実行する必要があります。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?