Angular Material2 & CDK make extensive use of coerceBooleanProperty in setters for boolean property inputs, but does not use Type consistently. Within their own code, in some cases we see both the getter return value and the setter argument as well as the internal property typed boolean
. For example:
@Input()
get myProperty(): boolean { return this._myProperty; }
set myProperty(value: boolean) { this._myProperty = coerceBooleanProperty(value); }
private _myProperty: boolean = false;
In other cases we see only the setter argument typed (this seems to be the most common pattern in Material2 & CDK):
@Input()
get myProperty() { return this._myProperty; }
set myProperty(value: boolean) { this._myProperty = coerceBooleanProperty(value); }
private _myProperty = false;
Sometimes we see getter and setter typed as any
:
@Input()
get myProperty(): any { return this._myProperty; }
set myProperty(value: any) { this._myProperty = coerceBooleanProperty(value); }
private _myProperty = false;
Sometimes there is no typing on either, but the internal property is deliberately typed boolean
:
@Input()
get myProperty() { return this._myProperty; }
set myProperty(value) { this._myProperty = coerceBooleanProperty(value); }
private _myProperty: boolean = false;
It seems to me that the first example - typing both the getter return value and the setter argument as boolean - is the 'correct' approach, because this forces the application or library typescript code to use the property properly. But the lack of consistency in their own code use makes me wonder if there's something I haven't considered. Or perhaps it is merely a matter of newer implementations improving upon older implementations.
What is the preferred pattern here and why?