In my programming project, I have created a model class with typed fields and a static method called generate
which returns these fields based on an object passed to it:
export class Message {
_id!: string
type!: 'foo' | 'bar'
static generate(properties): X {
return {
_id: properties.test.id,
type: properties.session._type_
}
}
}
I am looking for a way to automatically generate the type X (which is the result of the generate
method) based on the field types defined in the class. In other words, I want to avoid having to define the types twice - once inside the class and again inside the type alias like in the following example:
type X = {
_id: string
type: 'foo' | 'bar'
}
Is there a method in TypeScript that allows me to validate the output of the generate
method based on the class's field types? Or perhaps inherit the types of the class fields from the X alias?