A common structure I use in TS is taking a plain JSON object definition and turning it into a class at runtime. For example:
export type LessonDef = {
id: string
title: string
slug: string
shortdesc: string
explanation: string
exercises: {
from: string
message: string
translation: string
hint?: string
feedback?: { [key: string]: string }
}[]
}
export class Lesson {
constructor(readonly def: LessonDef) {
Object.assign(this, def)
}
// Additional methods go here
}
The problem is that the type system doesn't understand the result of the Object.assign. How can I tell TypeScript that Lesson extends the type of LessonDef?