Is it possible to type an array of strings in such a way that the array can only be a valid property path in a given object? The type definition should work for all deeply nested objects.
Example:
const object1 = {
someProperty: true
};
const object2 = {
nestedObject: object1,
anotherProperty: 2
};
type PropertyPath<Type extends object> = [keyof Type, ...Array<string>]; // <-- this needs to be improved
// ----------------------------------------------------------------
let propertyPath1: PropertyPath<typeof object1>;
propertyPath1 = ["someProperty"]; // works
propertyPath1 = ["doesntExist"]; // should not work
let propertyPath2: PropertyPath<typeof object2>;
propertyPath2 = ["nestedObject", "someProperty"]; // works
propertyPath2 = ["nestedObject", "doesntExist"]; // should not work
propertyPath2 = ["doesntExist"]; // should not work
In the answer to the question this duplicates you can use the recursive
Paths<>
orLeaves<>
type aliases, depending on whether or not you want to support all paths that start at the root and end anywhere in the tree (Paths<>
) or if you want to only support paths that start at the root and end at the leaves of the tree (Leaves<>
):I'll assume it's
Paths
but you can change it toLeaves
if that fits your use case. Here's the behavior you get, which matches what you asked for:Okay, hope that helps; good luck!
Link to code