Search code examples
angulartypescript

Subclassing FormControl triggers TS2510 - Base constructors must all have the same return type


I was trying to subclass Angular's FormControl when I ran into this error : TS2510 : Base constructors must all have the same return type

import { FormControl } from '@angular/forms';

export class Control<T = any> extends FormControl<T> {}

I suspect ɵFormControlCtor to be at fault.

  • What is the reason behind this message ?
  • Is there a clean fix (other than what is suggested in here) ?

Playground


Edit:
Extending the AbstractControl does not trigger this error:

import { AbstractControl } from '@angular/forms';

export class Control<T> extends AbstractControl<T> {

  patchValue(): void { }
  reset(): void { }
  setValue(): void { }

}

Playground


Solution

  • Long story short,

    FormControl is currently defined as a class expression of type ɵFormControlCtor with an interface with multiple overloads for the constructor, not a proper class.

    As pointed by Andrew Allen, this is due to TS's inability to have constructor overloads with different types. (#35387).