Image I have this object:
const data = {
foo: "foo",
bar: "bar",
layer1: {
foo: "foo",
layer2: {
foo: "foo",
bar: "bar",
},
},
};
How could I define a union type of string
s that has all possible paths (= I mean descending property chains such as foo.layer1.foo
or foo.layer1.layer2.bar
or also just foo
, ...)
I tried lumping together random pieces but can't get any further and don't know how I could do this dynamically... Is there any clever way of doing this?
type Path =
`${keyof typeof data}.${keyof typeof data.layer1}.${keyof typeof data.layer1.layer2}`;
This is would the type should look like ideally for data
:
type Path = "foo" | "bar" | "layer1.foo" | "layer1.layer2.foo" | "layer1.layer2.bar";