3
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の対応

3
Posted at

はじめに

  • TypeScriptのclassをはじめて書いた

問題

  • 以下のソースで erasableSyntaxOnly になる
export class Record {
  constructor(
    public id: string,
    public title: string,
    public time: string,
  ) {}

  static newRecord = (id: string, title: string, time: string) =>
    new Record(id, title, time);
}
  • 以下に変更して TS2339: Property id does not exist on type Record になる
export class Record {
  constructor(id: string, title: string, time: string) {
    this.id = id;
    this.title = title;
    this.time = time;
  }

  static newRecord = (id: string, title: string, time: string) =>
    new Record(id, title, time);
}

解決

  • TypeScript固有の削除可能な構文を使わないようにして、それに合わせてプロパティを定義して解決
export class Record {
  public id: string;
  public title: string;
  public time: string;

  constructor(id: string, title: string, time: string) {
    this.id = id;
    this.title = title;
    this.time = time;
  }

  static newRecord = (id: string, title: string, time: string) =>
    new Record(id, title, time);
}

終わりに

erasableSyntaxOnlyの概要

書き方の参考

全体像

JavaScriptのClass

プロパティの宣言場所

3
0
1

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
3
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?