Reputation: 4174
I've created a class that extends native angular service, and used class provider to override it.
Extended service
@Injectable()
export class ExtendedFormBuilder extends FormBuilder {
control(
formState: any,
validator: any = null,
asyncValidator: any = null,
errorConfig: any = null
): ExtendedFormControl {
return new ExtendedFormControl(formState, validator, asyncValidator, errorConfig);
}
}
part of app.module
{
provide: FormBuilder,
useClass: ExtendedFormBuilder
}
However intellisense in my component still thinks I'm using old FormBuilder
class.
this.fb.control('Joe', [
Validators.required,
Validators.minLength(2)
], null, { // Error! `FormBuilder.control` takes only 3 params, not 4.
minLength: 2
})
Is there any way to get around this, with usage of provide class?
One thing to note, is that code is working. What is not working is the typescript type checking, that is making my code unable to build.
https://angular.io/docs/ts/latest/cookbook/dependency-injection.html#useclass
Upvotes: 1
Views: 56
Reputation: 657741
The static type information only allows to derive FormBuilder
. If you "know", at runtime it will actually be an ExtendedFormBuilder
, you can cast it
class MyComponent {
ExtendedFormBuilder fb;
constructor(fb:FormBuilder) {
this.fb = fb as ExtendedFormBuilder;
}
Upvotes: 1