Typescript: define type of an object -


i want define type of object literal, key valye pairs, below. in 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'   } }; 

you're 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 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 code more readable in opinion (when compared inline way of declaring type)


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -