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

Proposal - ReactiveForms: add AbstractControl.getChild<T> method #17000

Open
snerks opened this issue May 24, 2017 · 6 comments

Comments

Projects
None yet
6 participants
@snerks
Copy link

commented May 24, 2017

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[ x ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

AbstractControl.get allows a flexible path parameter, for specifying the required descendent AbstractControl to return.

Expected behavior

AbstractControl.getChild<T>(key: keyof T) would allow additional type safety, in the case where the required AbstractControl is a direct child of the instance AbstractControl-derived parent.

Here, T refers to the type of the data model, i.e. the type of AbstractControl.value.

In fact, this method is probably only suitable for FormGroup.

Further, the following signature:
AbstractControl.getChild<T, C extends AbstractControl>(key: keyof T): C | null
would allow the caller to indicate the expected sub-type of AbstractControl to be returned.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Reduce the risk of typos in the string value passed to the proposed getChild method.

Please tell us about your environment:

  • Angular version: 4.1.3
  • Browser: [ all ]
  • Language: [ TypeScript 2.X ]

  • Node (for AoT issues): node --version =

@Toxicable

This comment has been minimized.

Copy link
Contributor

commented May 27, 2017

duplicate #13721

@snerks

This comment has been minimized.

Copy link
Author

commented May 31, 2017

I agree that this issue is similar to #13721.

However, I could not see any usage of the keyof T constraint, which aims to detect a typo in calling code, in the related Pull Request.

@Toxicable

This comment has been minimized.

Copy link
Contributor

commented May 31, 2017

I have a change that uses key look up on mapped types for that PR for the FormGroup constructor but running into a small issue with FormArray being allowed to have members of different types.
Either way, since the method you're talking about is on AbstractControl both FormControl and FormArray get this method in which, unless I misunderstand keyof usage, you cannot use it keyof lookup in those situations

@snerks

This comment has been minimized.

Copy link
Author

commented May 31, 2017

@Toxicable - thanks for a quick reply.

On reflection, I added (see text towards the bottom of my issue):

In fact, this method is probably only suitable for FormGroup.

I imagined that the "inward" (keyof constraint) implementation would look something like this (on FormGroup):

getChild<T>(key: keyof T): AbstractControl | null {
  return this.get(key);
}

Usage:

interface Customer {
  firstName: string;
  lastName: string;
}

const customerFormGroup = new FormGroup({
  firstName: "",
  lastName: ""
});

const firstNameAbstractControl = customerFormGroup.getChild<Customer>("firstName"); // works
const lastNameAbstractControl = customerFormGroup.getChild<Customer>("lastWithTypoName"); // compile-time error: lastWithTypoName does not exist on Customer

The "outward" constraint on the return type AbstractControl subtype would follow your Pull Request's approach.

@rpbeukes

This comment has been minimized.

Copy link

commented Oct 30, 2017

Here is an alternative while we wait for a better implementation - Angular Typesafe Reactive Forms

@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018

@Da13Harris

This comment has been minimized.

Copy link

commented Oct 12, 2018

I have a change that uses key look up on mapped types for that PR for the FormGroup constructor but running into a small issue with FormArray being allowed to have members of different types.
Either way, since the method you're talking about is on AbstractControl both FormControl and FormArray get this method in which, unless I misunderstand keyof usage, you cannot use it keyof lookup in those situations

@Toxicable I'm wondering if the issue of different types in a FormArray can be handled using TypeScript tuples?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.