1

I'm trying to follow an example from Adam Freeman's book on Angular and now stuck with Using a Form Model for Validation.

I have a similar issue as in Angular Typescript FormGroup assigning value after submit.

However, the accepted answer in that post didn't actually help me, because I got another error message: "Type 'any' is not assignable to type 'never'.ts(2322)".

So, I have an object of class Product:

export class Product {
    constructor(public id?: number,
    public name?: string,
    public category?: string,
    public price?: number) {}
}

In the form class I'm trying to do the following: enter image description here

Why does TS state: this.newProduct[c] is of type 'never'? And how do I make it work?

2
  • If you give us code instead of images, or even a minimal reproduction on Stackblitz it is easier to see what is wrong and help you. Commented May 14, 2022 at 17:58
  • If the answer sufficiently addressed your question and/or solved your problem, please consider also accepting it. Thank you, take care and have fun on SO. Commented May 31, 2022 at 22:16

2 Answers 2

1

Why are you looking for the values looping over the controls?

You should be able to just do this:

const newProduct = formGroup.value

If not, either you built up your form wrong, or your model isn't right.

But if you can loop over the keys and assign them to your newProduct class member (which you do not need) it looks to me like just assigning the forms value property should work fine. I think you're over complicating things.

By not having the newProduct class member you do not need to concern yourself with creating a new Product after you submit the form. That is what the reset on the formgroup is for.

Why you are flipping a boolean 'formSubmitted' is also unclear to me, when you set it to true, you are actually not sure if you actually did submit it. You can't be sure until you make the actual call. And u immediately set it to false again, even when the call were to fail.

Sign up to request clarification or add additional context in comments.

Comments

0

this.newProduct[c] makes no sense to me, what exactly are you trying to achieve here? Product is a class and you're using it like an array, hence it being of type never.

1 Comment

Make sense when using Object.keys()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.