I have developed an Angular app where I have configured the following:
"angularCompilerOptions": {
"strictInjectionParameters": true,
"fullTemplateTypeCheck": true,
"strictTemplates": true
}
As a result, all input and output are not type checked.
While this works well for most of the app, there are some inputs that require type checking. For example, consider this select input:
.html
<app-select
[useSearch]="true"
[formControlName]="'country'"
(valueChange)="setSelectedCountry($event)" <=== $event is of type unknown
>
<app-option
*ngFor="let country of locations$ | async"
[name]="'COUNTRIES.' + country.code | translate"
[value]="country.code" <=== this is of type Country
></app-option>
</app-select>
.ts
setSelectedCountry(code: Country) {
this.store.dispatch(loadLocationRequest({ payload: { code } }));
this.selectedLocation$ = this.store.pipe(select(getLocationByCode(), { code }));
}
For such cases, where I have multiple types of values for a select input, the current setup is:
@Input()
get value(): unknown | unknown[] {
return this.pValue;
}
set value(newValue: unknown | unknown[]) {
if (newValue !== this.pValue) {
this.pValue = newValue;
this.writeValue(newValue);
}
}
Now, I see two possible solutions:
- Avoid using ngModel like this
[(value)]="country"
and instead implement a custom method for type checking in all components that use a select input. - Create a type for each value type used in the select input and cast them accordingly.
However, I would like to find a simpler solution specifically for these cases. Is it possible to pass a generic type to a component via input so that it returns the type of the generic I passed? For example: <app-select<string>>
Is it feasible to create a pipe that can cast to a generic value without the need to create a pipe for each type like string
or number
?
Lastly, is it possible to selectively ignore certain type checks?