Reputation: 3728
I want to define the type of an Object literal, with key valye pairs, like below. In any way can't manage this. Please help.
export const endPoints: {name: string: {method: string; url: string;}} = {
allFeed: {
method: 'GET',
url: 'https://www.yammer.com/api/v1/messages.json'
},
topFeed: {
method: 'GET',
url: 'https://www.yammer.com/api/v1/messages/algo.json'
},
followingFeed: {
method: 'GET',
url: 'https://www.yammer.com/api/v1/messages/following.json'
},
defaultFeed: {
method: 'GET',
url: 'https://www.yammer.com/api/v1/messages.json/my_feed.json'
}
};
Upvotes: 2
Views: 497
Reputation: 164417
You're very close, should be:
const endPoints: { [name: string]: { method: string; url: string; } } = {
allFeed: {
method: 'GET',
url: 'https://www.yammer.com/api/v1/messages.json'
},
...
};
You can also use interfaces:
interface EndPoint {
method: string;
url: string;
}
interface EndPointMap {
[name: string]: EndPoint;
}
const endPoints: EndPointMap = {
...
}
Or types:
type EndPoint = {
method: string;
url: string;
}
type EndPointMap = {
[name: string]: EndPoint;
}
const endPoints: EndPointMap = {
...
}
Which makes the code more readable in my opinion (when compared to the inline way of declaring the type)
Upvotes: 2