Within the Angular framework, I have implemented a component input that allows for setting options, specifically of type IOptions
. The setter function does not require complete options as it will be merged with default options. Therefore, it is typed as Partial<IOptions>
.
const DEFAULT_OPTIONS = { prop1: false, prop2: 123 };
export class MyComponent {
_options: IOptions;
get options(): IOptions {
return this._options;
}
@Input('config')
set options(userOptions: Partial<IOptions>) {
this._options = { ...DEFAULT_OPTIONS, ...userOptions };
}
}
However, the getter is expected to always return a complete object of type IOptions
, leading to a compiler error regarding the mismatch between getter and setter types. How can this issue be resolved?