Is there a way to establish a relationship between two types in Typescript? For instance, I need to ensure that the TypeContent
in the example below is appropriate for the type T.
export type DataReport<T extends Type, TypeContent> = {
id: number
createdAtDateTime: Iso8601DateTime
contentType: T
content: TypeContent // should depend on T somehow
}
If we were using a JavaScript map, it might look something like this:
TypeMap = {
Questionnaire: { id: string, items: QuestionnaireItem[] }
BloodSample: { dueDate: Iso8601DateTime }
}
const TypeContent = TypeMap[Type] // Type = BloodSample or Questionnaire
Can this concept be translated into TypeScript effectively?