-
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
TypeError for FromGroup.removeControl #47306
Comments
This is working as expected due to how typed forms has been designed. If you want to remove a control, then you'd need to declare an explicit control type where the interface MyControlType {
a?: FormControl<string>;
} and then use that type when creating the Alternatively, you could switch to the untyped API by importing |
@JoostK That is a valid point, and I am sorry for giving a wrong example. Consider this code: const controls1: { a?: FormControl<number> } = {};
const group1 = new FormGroup(controls1);
group1.removeControl('a');
const controls2: { [key: string]: FormControl<number> } = {};
const group2 = new FormGroup(controls2);
group2.removeControl('a');
const controls3: Record<string, FormControl<number>> = {};
const group3 = new FormGroup(controls3);
group3.removeControl('a');
const controls4: { a: FormControl<number>; [additional: string]: FormControl<number> } = {
a: new FormControl(3, { nonNullable: true }),
b: new FormControl(3, { nonNullable: true }),
};
const group4 = new FormGroup(controls4);
group4.removeControl('b'); Only the first example works, while the last three show the same type error. It works as expected for 2 and 3 when adding const controls2: { [key: string]: FormControl<number> } | {} = {};
const group2 = new FormGroup(controls2);
group2.removeControl("a");
const controls3: Record<string, FormControl<number>> | {} = {};
const group3 = new FormGroup(controls3);
group3.removeControl('a'); |
In that case I think you're looking for |
Thank you, that is the correct solution! |
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. |
Which @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
FormControl.removeControl shows type error when TypeScript strictNullChecks is enabled.
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/angular-hy5qkn?file=tsconfig.json,src%2Fapp%2Fapp.component.ts
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
No response
The text was updated successfully, but these errors were encountered: