Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncValidatorFn can be passed as ValidatorFn to FormControl without warning #48676

Closed
JeanMeche opened this issue Jan 10, 2023 · 2 comments
Closed

Comments

@JeanMeche
Copy link
Member

JeanMeche commented Jan 10, 2023

Which @angular/* package(s) are relevant/related to the feature request?

forms

Description

Today the one of the 5 constructors of FormControl looks like this :

  new<T = any>(
      value: FormControlState<T>|T,
      validatorOrOpts?: ValidatorFn|ValidatorFn[]|FormControlOptions|null,
      asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl<T|null>;

The problem is that the compilator won't mind when passing an AsyncValidatorFn as 2nd parameter but the validation will always error since AsyncValidatorFn doesn't return null.

const foo: AsyncValidatorFn = (): Observable<{} | null> => {
  return of({});
};
const ctrl = new FormControl(3, foo); // compiler OK but validation won't work. 

The root case is that ValidatorFn returns a very loose type ValidationErrors : { [key: string]: any}. Observable<any> or Promise<any> are valid ValidationErrors.

Repro on Stackblitz

Proposed solution

Improve the typings of ValidationErrors or somehow make AsyncValidatorFn & ValidatorFn incompatible for example: with type ValidatorFn = (control: AbstractControl) => (ValidationErrors & { then?: never, subscribe?: never}) | null;

Demo on playground

Alternatives considered

Devs should be better at reading the apis :(

JeanMeche added a commit to JeanMeche/angular that referenced this issue Jan 10, 2023
With this commit, AsyncValidatorFn cannot be passed as ValidatorFn  anymore in FormControl.

fixes: angular#48676
JeanMeche added a commit to JeanMeche/angular that referenced this issue Jan 10, 2023
With this commit, AsyncValidatorFn cannot be passed as ValidatorFn  anymore in FormControl.

fixes: angular#48676
JeanMeche added a commit to JeanMeche/angular that referenced this issue Jan 10, 2023
With this commit, AsyncValidatorFn cannot be passed as ValidatorFn  anymore in FormControl.

fixes: angular#48676
@ngbot ngbot bot added this to the needsTriage milestone Jan 11, 2023
@dylhunn
Copy link
Contributor

dylhunn commented Jan 24, 2023

Devs should be better at reading the apis :(

💀💀

JeanMeche added a commit to JeanMeche/angular that referenced this issue Jan 24, 2023
With this commit, AsyncValidatorFn cannot be passed as ValidatorFn  anymore in FormControl.

fixes: angular#48676
JeanMeche added a commit to JeanMeche/angular that referenced this issue Mar 25, 2023
With this commit, AsyncValidatorFn cannot be passed as ValidatorFn  anymore in FormControl.

fixes: angular#48676
JeanMeche added a commit to JeanMeche/angular that referenced this issue Mar 26, 2023
With this commit, AsyncValidatorFn cannot be passed as ValidatorFn  anymore in FormControl.

fixes: angular#48676
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants