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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

An ability to define an interface for a FormControl value #29901

Closed
constantant opened this Issue Apr 15, 2019 · 2 comments

Comments

Projects
None yet
3 participants
@constantant
Copy link

constantant commented Apr 15, 2019

馃殌 feature request

Relevant Package

This feature request is for @angular/forms

Description

I would like to get an ability to define an interface for a FormControl value. It will be very helpful in templates

Describe the solution you'd like

It should look like
export interface MyType {
  a: string;
  b: number;
}
@Component({
  template: `<div>{{control.value?.a}}</div>`
})
export class MyComponent {
  control: FormControl<MyType> = new FormControl<MyType>({
    a: 'some data',
    b: 1
  });
  group: FormGroup<MyType>= new FormGroup({
    a: new FormControl<string>,
    b: new FormControl<number>
  });
  array: FormArray<MyType[]>= new FormArray([
    {
      a: 'some data',
      b: 1
    }
  ]);
  fbControl: FormControl<MyType> = this.fb.control<MyType>({
    a: 'some data',
    b: 1
  });
  fbGroup: FormGroup<MyType> = this.fb.group<MyType>({
    a: ['some data'],
    b: [1]
  });
  fbArray: FormArray<MyType[]> = this.fb.array<MyType[]>([
    {
      a: 'some data',
      b: 1
    }
  ]);
  constructor(private readonly fb: FormBuilder){}
}

It will allow to get understanding which type a value has

<div>{{control.value?.a}}</div>

Describe alternatives you've considered

Some links:

https://stackoverflow.com/questions/55651907/how-to-define-an-interface-for-a-formcontrol-value
https://itnext.io/help-keep-angular-forms-type-safe-ca35f7b2edf8

@trotyl

This comment has been minimized.

Copy link
Contributor

trotyl commented Apr 15, 2019

Duplicate of #13721

@kara

This comment has been minimized.

Copy link
Contributor

kara commented Apr 16, 2019

Thanks for the issue, @constantant, but as @troytl mentions, we have an existing issue in #13721. Closing this as a duplicate.

@kara kara closed this Apr 16, 2019

@kara kara added the comp: forms label Apr 16, 2019

@ngbot ngbot bot added this to the needsTriage milestone Apr 16, 2019

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