One technique I frequently use in TypeScript involves transforming a plain JSON object definition into a class during runtime. Here's an 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
}
An issue arises when the type system does not recognize the outcome of Object.assign
. How can I inform TypeScript that Lesson
is an extension of the type LessonDef
?