I’m a beginner at NestJs and I want to create a dto of the following struct:
I want to create an API that can return this object using DTOs.
export let Week = [ { DayName : "TuesDay", TimeZone: [ { First: { Start: 18, end: 19, isReserved: false, reserver: "someone" }, Second: { Start: 19, end: 20, isReserved: false, reserver: "someone" }, Third: { Start: 20, end: 21, isReserved: false, reserver: "someone" } } ] }, { DayName : "Wednesday", TimeZone: [ { First: { Start: 18, end: 19, isReserved: false, reserver: "someone" }, Second: { Start: 19, end: 20, isReserved: false, reserver: "someone" }, Third: { Start: 20, end: 21, isReserved: false, reserver: "someone" } } ] }, { DayName : "Friday", TimeZone: [ { First: { Start: 18, end: 19, isReserved: false, reserver: "someone" }, Second: { Start: 19, end: 20, isReserved: false, reserver: "someone" }, Third: { Start: 20, end: 21, isReserved: false, reserver: "someone" } } ] } ]
I tried the following dto but I got an error in the service provider :
I create 3 classes OneSessionResponseDto, SessionsResponseDto, and WeekResponseDto so I can use them as a dto of the object above.
export class OneSessionResponseDto { start: number; end: number; isReserved: boolean; reserver: string; } export class SessionsResponseDto { First: OneSessionResponseDto; Second: OneSessionResponseDto; Third: OneSessionResponseDto; } export class WeekResponseDto { DayName: string; TimeZone: SessionsResponseDto; }
this is the service provider class:
@Injectable() export class AppService { private week = Week; getWeek() : WeekResponseDto[] { return (this.week); <= the error } }
I got this error here:
is missing the following properties from type ‘SessionsResponseDto’: First, Second, Third
Advertisement
Answer
You almost have it. You need to set TimeZone
‘s type to SessionsResponseDto[]
because it’s an array. Then change OneSessionResponseDto#start
to OneSessionResponseDto#Start
(the capitalization is important). You can see a working solution in the typoescript playground here