I have a class with a various set of optional parameters when instantiated. I would like to be sure that the user input ONLY the available parameters option, and also add a default value if nothing is passed.
I come with this
export enum HexagoneType {
POINTY,
FLAT
}
export interface HexagoneOptions {
center?: Vector2;
size?: number;
color?: string;
type?: HexagoneType;
}
export default class Hexagone {
ctx: CanvasRenderingContext2D;
center: Vector2;
size: number;
color: string;
type: HexagoneType;
constructor(ctx: CanvasRenderingContext2D, options?: HexagoneOptions) {
this.ctx = ctx;
this.center = (options && options.center) ? options.center : new Vector2();
this.size = (options && options.size) ? options.size : 100;
this.color = (options && options.color) ? options.color : "red";
this.type = (options && options.type) ? options.type : HexagoneType.POINTY;
}
}
But is there something shorter ?