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

fix(forms): FormBuilder.group return right type with shorthand arguments #48084

Conversation

JeanMeche
Copy link
Member

@JeanMeche JeanMeche commented Nov 16, 2022

Extract AbstractControlOptions from type when used in shorthand argumentson FormBuilder.groups

Fixes #48073

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

Does this PR introduce a breaking change?

  • Yes
  • No

@JeanMeche JeanMeche force-pushed the bugfix/formbuild-groups-typings-48073 branch 2 times, most recently from 686dbb9 to 861160a Compare November 16, 2022 16:45
@AndrewKushnir AndrewKushnir requested review from dylhunn and removed request for AndrewKushnir November 16, 2022 17:38
@dylhunn dylhunn added area: forms forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray. labels Nov 16, 2022
@ngbot ngbot bot added this to the Backlog milestone Nov 16, 2022
@dylhunn
Copy link
Contributor

dylhunn commented Nov 16, 2022

@JeanMeche Thanks for your PR! However, this is actually not a complete solution. Consider the case of the other AbstractControlOptions fields:

const form = b.group({
  shorthand2: [5, {updateOn: 'blur'}],
});

expect(((form.get('shorthand2')!.value ?? 0) + 0)).toEqual(5);

@JeanMeche
Copy link
Member Author

JeanMeche commented Nov 16, 2022

@dylhunn Correct me if i'm wrong, but [5, {updateOn: 'blur'}] works as long as it's typed correctly : {updateOn: 'blur'} as const instead of blur being just a string !

Or would you prefer having FormControl<Exclude<U, ValidatorConfig | (AbstractControlOptions | {updateOn: string})> | N> (which would make less sense to me) ?

@dylhunn
Copy link
Contributor

dylhunn commented Nov 17, 2022

@JeanMeche Ah, I see, it doesn't work because updateOn is inferred with a string key rather than a union of literals.

I think we don't want users tripping over the same mistake I made. Perhaps we could define an interface local in form_builder.ts:

interface PermissiveAbstractControlOptions extends Omit<AbstractControlOptions, 'updateOn'> {
  updateOn?: string;
}

where updateOn is just a string, and that way people aren't surprised by the type when trying to use updateOn with FormBuilder.group().

What do you think?

…ters.

Extract AbstractControlOptions from type when used in shorthand parameters on FormBuilder.group

Fixes 48073
@JeanMeche JeanMeche force-pushed the bugfix/formbuild-groups-typings-48073 branch from 861160a to 2e52ec3 Compare November 17, 2022 08:58
@JeanMeche
Copy link
Member Author

@dylhunn Sounds reasonable to me !

I updated the PR !

@dylhunn dylhunn added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Nov 17, 2022
@dylhunn
Copy link
Contributor

dylhunn commented Nov 17, 2022

This PR was merged into the repository by commit d321880.

@dylhunn dylhunn closed this in d321880 Nov 17, 2022
dylhunn pushed a commit that referenced this pull request Nov 17, 2022
…ters. (#48084)

Extract AbstractControlOptions from type when used in shorthand parameters on FormBuilder.group

Fixes 48073

PR Close #48084
@dylhunn
Copy link
Contributor

dylhunn commented Nov 17, 2022

@JeanMeche Thanks for the PR! This will be out in 15.0.1 next week.

@dylhunn
Copy link
Contributor

dylhunn commented Nov 23, 2022

@JeanMeche This is now released.

@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 Dec 24, 2022
@JeanMeche JeanMeche deleted the bugfix/formbuild-groups-typings-48073 branch January 4, 2023 22:59
trekladyone pushed a commit to trekladyone/angular that referenced this pull request Feb 1, 2023
…ters. (angular#48084)

Extract AbstractControlOptions from type when used in shorthand parameters on FormBuilder.group

Fixes 48073

PR Close angular#48084
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray. target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Form typing does not work with form builder group shorthand
2 participants