Sobes.tech
Junior — Senior

Type definitions for objects with fixed endpoint keys

livecode

Task condition

It is necessary to develop a type that correctly describes two different objects while preserving the exact types of keys within the endpoints property. Standard approaches like key: string or Record<string, ...> are not suitable because they blur the specific method names.

It is assumed that the set of endpoint keys is known at compile time for each object:

  • for vtemplateObjectgetVtemplates and postVtemplates;
  • for reportObjectgetReports and putReports.

The task is to describe types so that TypeScript "knows" exactly which properties are inside endpoints, without changing the base object types.

//-- 1 -- Defines the structure of objects, does not know about specific objects and their methods

//-- 2 -- Cannot change the base type but knows about the structure of the object it types

const vtemplateObject = {
    entity: "vtemplate",
    endpoints: {
        getVtemplates: {
            method: "GET",
            url: "vtemplate"
        },
        postVtemplates: {
            method: "POST",
            url: "vtemplate"
        }
    }
}

const reportObject = {
    entity: "report",
    endpoints: {
        getReports: {
            method: "GET",
            url: "report"
        },
        putReports: {
            method: "PUT",
            url: "report"
        }
    }
};

Build a type (or set of types) that will accept such objects and preserve the exact types of their endpoints without resorting to generic Record<string, ...> and without changing the object structure.