In my Angular component, I have an input that typically works with an array of strings:
@Input() names: string[]
<my-comp [names]="['Adam', 'Betty']"></my-comp>
However, I would like to offer an alternative syntax where the user can provide a single value without needing to include brackets:
<my-comp names="Adam"></my-comp>
To achieve this, I aim to handle it in the setter as follows:
private _names: string[];
@Input() set names(value: string[] | string) {
this._names = Array.isArray(value) ? value : [value];
}
get names(): string[] {
return this._names;
}
Despite this solution, TSLint brings up error TS2380 which mentions that the accessors should have the same type.
So, my question is: Is there an elegant TypeScript approach to address this scenario?