I have a constant Object called STUDY_TAGS with specific properties
const STUDY_TAGS ={
InstanceAvailability: { tag: "00080056", type: "optional", vr: "string" },
ModalitiesinStudy: { tag: "00080061", type: "required", vr: "string" },
ReferringPhysiciansName: { tag: "00080090", type: "required", vr: "string" },
NumberofStudyRelatedSeries: {
tag: "00201206",
type: "required",
vr: "number",
}
};
I want to determine the return type of each object based on its 'vr' value. However, when I check the typeof STUDY_TAGS, all key-value pairs appear the same:
InstanceAvailability: {
tag: string;
type: string;
vr: string;
};
Is there a way to make TypeScript keep the string literals instead of generalizing them to just 'string'?
I initially tried defining the Object with the type Record<string, {tag: string, type: string, vr: "string" | "number"}>, but it didn't solve the issue as typeof STUDY_TAGS showed:
Record<string, {
tag: string;
type: string;
vr: "string" | "number";
}
I am stuck and unsure how to tackle this problem. There must be a way to infer the return type based on an object having one of two string values, right? Ultimately, my goal is to create a function that takes in the object and determines the returned type based on the 'vr' value.
function doSomething<Type extends "number" | "string">({tag, type, vr} : {tag : string, type : string, vr: Type}) : Type extends "number" ? number : string
{
if(vr === "string") return "test";
return 0;
}