Answer the question
In order to leave comments, you need to log in
Why can't assign values via for in in typescript?
What's the right way to do things like this?
constructor(recipeDetails: { title?: string, description?: string } = { title: "", description: "" }) {
for (let key in recipeDetails) {
this[key.toString()] = recipeDetails[key];
}
}
Answer the question
In order to leave comments, you need to log in
constructor(recipeDetails: Details = { title: "", description: "", ingredients: [] }) {
for (let key in recipeDetails) {
(this[key as keyof Details] as Details[keyof Details]) = recipeDetails[key as keyof Details];
}
}
interface Details { title?: string, description?: string, ingredients?: RecipeIngredient[]}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question