Seeking guidance on writing clean code, particularly regarding a property with a backing field:
get value(): string { return this._value; }
set value(v: string) { this._value = v; }
private _value?: string;
It has come to my attention that this code is considered invalid because _value
is undefined by default. Must I explicitly specify the type as string | undefined
each time? This seems cumbersome. Additionally, if I need to allow null values, it would require further modification:
get value(): string | undefined | null { return this._value; }
set value(v: string | null) { this._value = v; }
private _value?: string | null;
Is there a more elegant solution to handle this scenario? Or is this the standard approach?