Given:
interface Example {
items: {
[k: string]: {
name: string;
value: {
subName: {
subValue: string;
};
};
};
};
};
It's possible to create a type for items
with a lookup type:
type Items = Example['items'];
But what if I want to get the type below the indexer?
type SubItems = Example['items']['']['value'];
The above won't work. Is there a way to access the type of the value
object?
This works:
type SubItems = Example["items"][string]["value"];
You can access it hierarchically. Please look at this:
interface Example {
items: {
[k: string]: {
name: string;
value: {
subName: {
subValue: string;
};
};
};
};
}
var example = {
items:{
'item':{
name:'1',
value:{
subName:{
subValue:'2'
}
}
}
}
} as Example
console.log(example['items']['item']['value']['subName']['subValue']); //2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With