I have an app in angular where I set
"angularCompilerOptions": {
"strictInjectionParameters": true,
"fullTemplateTypeCheck": true,
"strictTemplates": true
}
So every input / output is not type checked.
It is good for most of the app, but I have some app input like (here I show select, but I also have a simple app-input that works the same) :
.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 }));
}
in the above, since I use my app-select for multiple selector with various value, it is type :
@Input()
get value(): unknown | unknown[] {
return this.pValue;
}
set value(newValue: unknown | unknown[]) {
if (newValue !== this.pValue) {
this.pValue = newValue;
this.writeValue(newValue);
}
}
Now, There is 2 solutions I see,
- I do not use the ngModel like this
[(value)]="country"and I make a method that typecheck in all my components that use a select: - I create a type for every type of value my select use and cast to it.
But I would like to have something easier for those case only.
Is it possible to pass a Generic type to a component via input or something, so that it return a type of the Generic I passed ?
like (ex : <app-select<string>>)
Is it possible to make a pipe that cast a to a generic value ? without having to make a pipe for each type ? string number etc... ?
Is it possible to ignore certain checks ?