-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
feat(forms): add generic typings to AbstractControl #20040
Conversation
e1f0154
to
16a256b
Compare
f98de13
to
4420bc9
Compare
Stop me if i'm wrong but,
I need to construct a form like this (without types) :
If i use typed forms, the property 'name' need to be FormArray
But, if FormGroup contructor take Property 'name' will be of type |
@Trophalaxeur Therefore |
@Toxicable great job on implementing this. |
@c69 untill there is a final API on this there is no need to write docs since the API could change before it lands. |
Nice work 👏 |
d50cad9
to
7f4444a
Compare
7f4444a
to
ecc26ae
Compare
Nice one @Toxicable! |
Nice work, any hope on merging this soon? |
Hi @Toxicable! This PR has merge conflicts due to recent upstream merges. |
1 similar comment
Hi @Toxicable! This PR has merge conflicts due to recent upstream merges. |
I really want this!!!! Can't wait to see this in action!!!! |
+1 |
I’m actually surprised that this hasn’t made its way into the framework yet :) Seems like a no-brainer that people want strongly typed functionality these days. Anyhow - me and my team is waiting eagerly on this one |
Yes Angular team is aware of that but mentioned during angular Connect 2018 that they are waiting for some TS bug to be fixed 🤷♂️ |
In case you wonder, it's blocked on microsoft/TypeScript#16229. |
Hello! class O<T=any> {
constructor(public array: T[]) { }
}
type Unwrap<T> = T extends O<infer U> ? U : T
declare let val: O<number> | O<string>;
declare function f<V extends O<any>, T=Unwrap<V> >(x: V): T;
const res = f(val); // works
type returnType = typeof res // string | number
f(1); // Argument of type '1' is not assignable to parameter of type 'O<any>'. This one also works. type Unwrap<T> = T extends AbstractControl<infer U> ? U : never
class AbstractControl<T extends any = any> {
value: T;
}
class FormGroup<T extends any> extends AbstractControl<T> {
constructor(
public controls: {[key in keyof T]: AbstractControl<T[key]>}
) {
super()
}
}
class FormArray<C extends AbstractControl<any> , T = Unwrap<C>> extends AbstractControl<T>{
constructor(
public controls: C[]
) {
super()
}
}
class FormControl<T extends any> extends AbstractControl<T>{
constructor(value: T) {
super()
this.value = value
}
}
let a = new FormArray([
new FormGroup({'c2': new FormControl('v2'), 'c3': new FormControl('v3')}),
new FormArray([new FormControl('v4'), new FormControl(5)])
]);
a.controls[0].value // string | number | { 'c2': string; 'c3': string; }
a.controls[1].value // string | number | { 'c2': string; 'c3': string; } I can experiment a bit with typescript tuples to get |
@IgorMinar |
Can't we just ask the guys who created https://github.com/no0x9d/ngx-strongly-typed-forms to allow to implement their way of strongly typing into angular? Of course there can be some improvements made thanks to new features of current TS but it is already great implementation with over 1000+ daily downloads on npm. |
Why is that a blocker? |
@kara are there any news about this? |
Possibly good news here... I cannot wait for strongly typed forms to be part of Angular. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
#13721 Currently there is no generic type support for
AbstractControl
Therefore
AbstractControl#value
will always return a value with type:any
What is the new behavior?
Provides type support for
AbstractControl#value
and related fields/methodsFor example:
In this example
control
will becontrol: FormControl<string>
value
will bevalue: string
changes
will bechanges: Observable<string>
Does this PR introduce a breaking change?
Duplicate of #16828 since I kinda messed up that branch
closes #13721