@Bensonの回答でコメントされているように、コードでこの例を使用しましたが、非常に便利であることがわかりました。ただしObject is possibly 'undefined'.ts(2532)
、クラス変数の型を使用して計算を試みたときにエラーが発生しました。疑問符が型になるためですAssignedType | undefined
。未定義のケースが後の実行で処理された場合、またはコンパイラの型強制で処理された場合でも<AssignedType>
、エラーを取り除くことができなかったため、引数をオプションにすることができませんでした。疑問符の params とクラス変数を使用して、引数の分離型を作成することを解決しました。疑問符なしで。冗長ですが、機能しました。
クラス method() でエラーが発生した元のコードは次のとおりです。以下を参照してください。
/** @class */
class Box {
public x?: number;
public y?: number;
public height?: number;
public width?: number;
// The Box class can work double-duty as the interface here since they are identical
// If you choose to add methods or modify this class, you will need to
// define and reference a new interface for the incoming parameters object
// e.g.: `constructor(params: BoxObjI = {} as BoxObjI)`
constructor(params: Box = {} as Box) {
// Define the properties of the incoming `params` object here.
// Setting a default value with the `= 0` syntax is optional for each parameter
const {
x = 0,
y = 0,
height = 1,
width = 1,
} = params;
// If needed, make the parameters publicly accessible
// on the class ex.: 'this.var = var'.
/** Use jsdoc comments here for inline ide auto-documentation */
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
method(): void {
const total = this.x + 1; // ERROR. Object is possibly 'undefined'.ts(2532)
}
}
const box1 = new Box();
const box2 = new Box({});
const box3 = new Box({ x: 0 });
const box4 = new Box({ x: 0, height: 10 });
const box5 = new Box({ x: 0, y: 87, width: 4, height: 0 });
そのため、変数はクラス メソッドで使用できません。たとえば、次のように修正すると、次のようになります。
method(): void {
const total = <number> this.x + 1;
}
今、このエラーが表示されます:
Argument of type '{ x: number; y: number; width: number; height: number; }' is not
assignable to parameter of type 'Box'.
Property 'method' is missing in type '{ x: number; y: number; width: number; height:
number; }' but required in type 'Box'.ts(2345)
あたかも引数バンドル全体がオプションではなくなったかのように。
したがって、オプションの引数を持つ型が作成され、クラス変数がオプションから削除された場合、引数はオプションになり、クラスメソッドで使用できるようになります。ソリューション コードの下:
type BoxParams = {
x?: number;
y?: number;
height?: number;
width?: number;
}
/** @class */
class Box {
public x: number;
public y: number;
public height: number;
public width: number;
// The Box class can work double-duty as the interface here since they are identical
// If you choose to add methods or modify this class, you will need to
// define and reference a new interface for the incoming parameters object
// e.g.: `constructor(params: BoxObjI = {} as BoxObjI)`
constructor(params: BoxParams = {} as BoxParams) {
// Define the properties of the incoming `params` object here.
// Setting a default value with the `= 0` syntax is optional for each parameter
const {
x = 0,
y = 0,
height = 1,
width = 1,
} = params;
// If needed, make the parameters publicly accessible
// on the class ex.: 'this.var = var'.
/** Use jsdoc comments here for inline ide auto-documentation */
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
method(): void {
const total = this.x + 1;
}
}
const box1 = new Box();
const box2 = new Box({});
const box3 = new Box({ x: 0 });
const box4 = new Box({ x: 0, height: 10 });
const box5 = new Box({ x: 0, y: 87, width: 4, height: 0 });
時間をかけて読んで、私が言おうとしている点を理解しようとする人からのコメントを歓迎します.
前もって感謝します。