Typescript is a TYPED superset of JavaScript that compiles into JavaScript, fine! It helps us to reduce some typos etc. I want to create an interface that will serve as an argument in a method. This interface needs to represent a treeview for parsing an object.
For example: w1[a2].x[b02].y.z
is the path to access the value of z
in myObject
const path = "w1[a2].x[b02].y.z";
const treeOfIdentifiers = {
"w1": {
key: "idLvlW",
"x": {
key: "idLvlX"
}
}
}
const myObject = {
w0: "Hello Root",
w1: [{
idLvlW: "a1",
x: [{
idLvlX: "b01",
y: {
z: "hello world from w1[a1].x[b01].y.z"
}
},
{
idLvlX: "b02",
y: {
z: "hello world from w1[a1].x[b02].y.z"
}
},
{
idLvlX: "b03",
y: {
z: "hello world from w1[a1].x[b03].y.z"
}
},
{
idLvlX: "b04",
y: {
z: "hello world from w1[a1].x[b04].y.z"
}
}
]
},
{
idLvlW: "a2",
x: [{
idLvlX: "b01",
y: {
z: "hello world from w1[a2].x[b01].y.z"
}
},
{
idLvlX: "b02",
y: {
z: "hello world from w1[a2].x[b02].y.z"
}
},
{
idLvlX: "b03",
y: {
z: "hello world from w1[a2].x[b03].y.z"
}
},
{
idLvlX: "b04",
y: {
z: "hello world from w1[a2].x[b04].y.z"
}
}
]
},
{
idLvlW: "a3",
x: [{
idLvlX: "b01",
y: {
z: "hello world from w1[a3].x[b01].y.z"
}
},
{
idLvlX: "b02",
y: {
z: "hello world from w1[a3].x[b02].y.z"
}
},
{
idLvlX: "b03",
y: {
z: "hello world from w1[a3].x[b03].y.z"
}
},
{
idLvlX: "b04",
y: {
z: "hello world from w1[a3].x[b04].y.z"
}
}
]
}
]
If I were to code using TypeScript, what would be the type or interface of treeOfIdentifiers
(excluding any
)? The main aspect to consider is making sure that each node of treeOfIdentifiers has the required property key
, especially since we are uncertain about the structure of both the treeOfIdentifiers
and the object being parsed!