I have the following array
const roads = [
"Alice's House-Bob's House",
"Alice's House-Cabin",
"Alice's House-Post Office",
...
];
And I want to define a type Graph that is an object and has name:value
In this case each string in the roads array is a direction where the string until '-' is the from and from there to the end the to. So I want the type to be an object of a name string and a value of an array of strings.
Something like this:
{
"Alice's House": [ "Bob's House", 'Cabin', 'Post Office' ],
...
}
Any ideas?
CodePudding user response:
You have at least 2 options to do this:
- Use index type
type Address {
[key: string]: string[]
}
- Use
Record
utility type
type Address = Record<string, string[]>