Home > Enterprise >  How to create a model with dynamic amount of the nested items TypeScript
How to create a model with dynamic amount of the nested items TypeScript

Time:12-22

I have a list of workouts. Each workout has nested exercises, e.g: Workout name: "Day 1" Exercises: push-ups, squats, etc.

Users should be able to create new exercises and delete those they don't need. So, I want to create a Workout model that will include these exercises. The problem is you never know how many of these exercises will be there.

I think I just don't fully understand the models. Could you advise, please? How the workout model should look like?

My target is that after the user will add a new Workout with exercises I will be able to do something like:

export interface Workout {
  name: string,
  exercises: [ Exercise1, Exercise2, Exercise3, ... ]
}

export interface Exercise {
  name: string,
  status: boolean
}

newWorkout: Workout = {name: "Day 1", exercises: [ {name: "push-ups", completed: true}, {name: "squats", completed: false} ]}

CodePudding user response:

Adding exercises as an array of Exercise in Workout should allow you to do that:

export interface Workout {
  name: string,
  exercises: Exercise[]
}

export interface Exercise {
  name: string,
  status: boolean
}

But please clarify what you mean by "The problem is you never know how many of these exercises will be there.". What is the problem you actually encountered / what is your question?

  • Related