1

I am trying to push formgroup in an formArray but it is giving me the error code which is

Argument of type 'FormGroup<{ name: FormControl<string | null>; amount: FormControl<number | null>; }>' is not assignable to parameter of type 'never'.ts(2345)"

Here is how i declared the Array

let recipeIngredients = new FormArray([]);

Initializing the form here

private initForm() {
    let recipeName = '';
    let recipeImagePath = '';
    let recipeDescription = '';
    let recipeIngredients = new FormArray([]);
    

    if (this.editMode) {
      //getting the recipe
      const recipe = this.recipeService.getRecipe(this.id);
      //assigning the values coming from recipes
      recipeName = recipe.name;
      recipeImagePath = recipe.imagePath;
      recipeDescription = recipe.description;
      if(recipe['_ingredients']){
        for( let ingredient of recipe.ingredients){
          

          recipeIngredients.push( // getting error here
            new FormGroup({
              'name': new FormControl(ingredient.name),
              'amount': new FormControl(ingredient.amount)
            })
          )
          
        }
      }

      this.recipeForm = new FormGroup({
        'name': new FormControl(recipeName),
        'imagePath': new FormControl(recipeImagePath),
        'description': new FormControl(recipeDescription)
        //'ingredients': recipeIngredients

      });
      
    }
5
  • How does error change when you go let recipeIngredients: FormArray = new FormArray([]); Commented Nov 7, 2022 at 14:54
  • 1
    @AndrewAllen it says Type 'FormArray<never>' is not assignable to type 'FormArray<any>'. Type 'any' is not assignable to type 'never' Commented Nov 7, 2022 at 15:02
  • 2
    Typescript thinks [] inside FormArray([]); is of type never[]. Try let recipeIngredients: FormArray = new FormArray<any>([]); Commented Nov 7, 2022 at 15:06
  • Are you on Angular 14, if so I'll post a fuller answer on how to correctly type these things Commented Nov 7, 2022 at 15:41
  • yes i am using Angular 14 @AndrewAllen Commented Nov 8, 2022 at 8:16

1 Answer 1

2

specify the type of the FormArray

items = new FormArray<FormGroup>([]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.