-
-
Notifications
You must be signed in to change notification settings - Fork 56
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: handle 'any' values properly in 'patchValue' #59
Conversation
@rafaelss95 what about taking the implementation from here? |
Hmm, let me see... doesn't it would be an overkill? I mean, Angular forms doesn't support Set, Map, only literal objects and arrays. Do you consider the proposed implementation in this PR fragile? |
Sure, I didn't mean "copy-paste" all of it, just the relevant parts... you can skip the |
Also, your fix broke some tests, please notice that |
I just noticed that and I may have found another bug (not really related by these changes) :( Look at this ( Edit: I found the bug. The reactive-forms/projects/ngneat/reactive-forms/src/lib/types.ts Lines 141 to 143 in ee41c40
... recognizes |
@rafaelss95 I see that, would you like to create another PR that fixes that? |
Done. Checkout it out in #60. |
@itayod Removing the unneeded parts of it ( export type PartialDeep<T> = T extends Primitive
? Partial<T>
- : T extends Map<infer KeyType, infer ValueType>
- ? PartialMapDeep<KeyType, ValueType>
- : T extends Set<infer ItemType>
- ? PartialSetDeep<ItemType>
- : T extends ReadonlyMap<infer KeyType, infer ValueType>
- ? PartialReadonlyMapDeep<KeyType, ValueType>
- : T extends ReadonlySet<infer ItemType>
- ? PartialReadonlySetDeep<ItemType>
- : T extends ((...arguments: any[]) => unknown)
- ? T | undefined
: T extends object
? PartialObjectDeep<T>
: unknown;
type PartialObjectDeep<ObjectType extends object> = {
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType]>
}; ... which produces exactly the same result as the one in this PR: export type DeepPartial<T> = T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T; |
there is still T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
} : Partial<T>; |
As
Besides this, this change makes the build fail:
|
@@ -348,7 +348,7 @@ export class FormGroup<T extends Obj = any, E extends object = any> extends NgFo | |||
tap(value => { | |||
if (!value) return; | |||
handleFormArrays(this, value, arrControlFactory); | |||
this.patchValue(value, { emitEvent: false }); | |||
this.patchValue(value as any, { emitEvent: false }); |
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.
do we have any reference of why this is failing now?
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.
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.
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.
sure, let's try to find out why might be typescript's bug if so let's reference the issue
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.
I don't think it's a Typescript bug, it happens because value
in this context is just typed as Obj
({ [key: string]: any }
), however patchValue
expects an Observable<DeepPartial<ControlsValue>>
or DeepPartial<ControlsValue>
and Obj
isn't assignable to ControlsValue
.
You can solve this problem or using any
(as you already did in (
return super.getError(errorCode as any, path) as E[K] | null; |
super.patchValue
directly.
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.
your explanation doesn't make sense at all, if that's true then how come it worked before? since you didn't change patchValue
signature...
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.
Well, I didn't change patchValue
signature directly, but indirectly. Anyway, as I had some more free time today I was able to fix it by changing the conditional in DeepPartial
. Let me know if we can finally move on with this. Regards.
@rafaelss95 LGTM |
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?
patchValue
doesn't handleany
properly.Issue Number: Fixes #58
What is the new behavior?
patchValue
handleany
properly.Does this PR introduce a breaking change?