-
Notifications
You must be signed in to change notification settings - Fork 78
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
Invalid type provided for FormArray #511
Comments
Update same situation is for IFormArray w/ formGroups in it (same example): interface IUser {
name: string;
hobbies: IFormArray<IHobby>; // Same incorrect behaviour for hobbies: IHobby[];
}
@Compoent({ })
class SomeComponent {
// ...
private _initForm(initialData: Partial<IUser> = { }): void {
const hobbiesFormGroups = (initialData?.hobbies ?? []).map((hobby) => this._formBuilder.group<IHobby>({
title: [hobby.title],
}))
this.form = this._formBuilder.group<IUser>({
name: [initialData.name],
hobbies: this._formBuilder.array(hobbiesFormGroups),
})
this.form.controls.hobbies.controls[0].controls // In runtime here we have controls for first hobby in array
// But in compile time we have TypeError by TS - there is no `controls` property as it is of type IAbstractControl
}
} fix: export interface IFormArray<Item> extends IAbstractControl<Item[]> {
controls: Item extends IAbstractControl<any> ? Item[] : Array<IAbstractControl<Item>>;
...
} in this case we'll need to cast formBuilder type manually class SomeComponent {
private readonly _formBuilder: IFormBuilder;
constructor(
_formBuilder: FormBuilder,
) { this._formBuilder = _formBuilder as IFormBuilder; }
} |
@mopcweb I really appreciate the suggested solution for the issue :). I think we should explore other possibilities to fix this issue because if we add this solution then existing package users will face project build issues. I wish to avoid the same. if you have any other solution to overcome the build issue then please share the same. I will also explore the possibilities to fix this issue without build failures for the existing users. |
Describe the bug
There is invalid types provided in case of formArray as part of formGroup
Expected Behavior
Types should be correct.
This could be achieved by using conditional types.
To Reproduce
Fix
use condition types in i-form-group.ts
Url (stackblitz or plunker or other)
Package Version
@rxweb/[email protected]
The text was updated successfully, but these errors were encountered: