はじめに
- 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
プロパティの宣言場所
