I am quite new to Typescript and I am really appreciating its type checking capabilities.
I need to understand if with its capabilities it is possible to check at compile time if the domain of a value is within a set of key names declared in the same object?
The following example will help to clarify my question. I have these object and type definitions:
type ElemField = { name: string }
type Elem = Record<string, ElemField>
type Relation = { path: string[] }
type MySchema = {
elem: Record<string, Elem>,
relations: Record<string, Relation>
}
const test: MySchema = {
elem: {
elem1: {
prop1: { name: 'string' },
},
elem2: {
prop2: { name: 'string' },
prop3: { name: 'string' },
},
elem3: {
prop4: { name: 'string' },
}
},
relations: {
relation1: {
path: ['elem2', 'elem1'],
},
relation2: {
path: ['elem3', 'elem1'],
}
}
}
I am wondering if it possible to check at compile time if the path array contains values only within the domain of the keys of the elements key at root level.
I cannot understand if with the usage of keyof, generics, or some other Typescript features it is possible to define such relation.
EDIT: Thanks to Shahriar Shojib I got a first valid solution.
But I am wondering if it is possible to achieve this without employing a function, but simply specifying the type for the object.