-
Notifications
You must be signed in to change notification settings - Fork 33
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(ControlsType): added typings for AbstractControls + FormArrays #139
Conversation
Looking into why tests fail builds were passing locally |
After going through the travis log it is not clear to me what the issue is. All tests / specs are marked as passed no failures reported. |
@ntziolis thanks for this PR, I took a quick look and like where this is going! 👏 To explain a bit what's happening on CI, the only command failing is:
So nothing to really worry about. We use The only command you need to run is: I'll make a better review ASAP 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good idea 👍 thanks again for contributing btw!
While we're at it, I think it'd be nice to also update TypedFormGroup
as currently I think it only handle type safety from formGroupControls but not the formGroup itself, does it?
formGroup.controls
can probably be of type ControlsType<FormInterface>
too. And setValue
, patchValue
, valueChanges
can also be improved on it :)
It would also be nice to edit the readme where needed to reflect those changes |
Not done yet, will ping when ready to review. |
@maxime1992 I have just realized the following:
This affects
What do you think? Any other options I overlooked? UPDATE: In our code we have extracted arrays into a dedicated sub form that only handles the array. This allowed us to create generic array value sub form base class that provides helper functions to handle array forms easily. This class again is based on a single value sub form |
|
… all others are types as well
All done. Let me know your thoughts. |
Anything I can do to help get this PR merged in? |
@maxime1992 this all looks great to me, happy for merge. No diff in the example repo so I’m fairly confident this is non-breaking downstream |
All done:
|
Caught 2 minor things, should be good to merge straight after that :)! |
Some food for thought:
While writing this I had another idea:
|
Oh you're right for
Thanks for bringing that up 👍
Yes, I feel like this would be the best approach here. @zakhenry any other suggestion? |
Yep I see the problem here, however I would recommend that we make the signature export type ControlsType<T> = {
[K in keyof T]-?: T[K] extends any[] ? TypedFormArray<T[K]> : TypedFormControl<T[K]> | TypedFormGroup<T[K]>;
}; it is a more realistic outcome - you can't construct an AbstractControl directly so I think it is better to be explicit and say that this key is a control or a group, who knows, you'll have to use a type-guard to test it. |
I really like this approach. @zakhenry picking up on an earlier comment I made: So we should really be typing this as follows: export type ControlsType<T> = {
[K in keyof T]-?: T[K] extends any[]
? TypedFormControl<T[K]> | TypedFormArray<T[K]>
: T[K] extends object
? TypedFormControl<T[K]> | TypedFormGroup<T[K]>
: TypedFormControl<T[K]>;
}; The thing is: For now I have updated the PR with the typings provided in your last comment . But wanted to make sure the earlier comment I made is better understood. |
FYI: The CI process seem to timeout sometimes and I have no way to re-trigger a build besides pushing a change. Same code with a space then runs through without a hitch. |
@ntziolis I gave this a go on our app at work and I noticed that something is odd when dealing with arrays. I've made a minimal repro:
When using |
I have an idea what the issue might be |
Oh cool. BTW you'll need to rebase as #145 got merged 👍 |
# Conflicts: # projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts
I just saw that on your PR. It appears to already have an issue reported on Cypress side: cypress-io/cypress#5965 I did re trigger your build 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zakhenry I think this is now good to go 🔥
🎉 This PR is included in version 5.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@ntziolis I'd like to post a Tweet to announce the better typings on ngx-sub-form and I'd like to give you back the credit for it 👍 Do you have a Twitter account that I can use? 😸 |
wupwup. much appreciated and yes I do, it's the same as my github handle. |
I tried this morning and it didn't work 😂 not sure what I did wrong but it's working now, thanks 👍 Will do asap! EDIT: done https://twitter.com/maxime1992/status/1232967540370018304 |
feat(ControlsType): added typings for AbstractControls + FormArrays # Conflicts: # projects/ngx-sub-form/src/lib/ngx-sub-form-utils.ts # projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts # projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts
Allow for partially typed form controls when accessing controls via
formGroupControls
. Typed methods are:setValue
patchValue
valueChanges
value
There is a caveat with this approach:
AbstractControl
anymore, instead it isTypedAbstractControl
FormControl
orFormGroup
is not possible. Instead one has to useas unknown
before casing toFormControl
orFormGroup
I feel this caveat is acceptable. Should you feel otherwise I suggest adding a third generic parameter to NgxSubForm that allows to specify which type should be used for
formGroupControls
so it would be up to the dev to choose to accept the above mention caveat.