2016-07-28 9 views
1

以下のように、キーのバイアペアでオブジェクトリテラルのタイプを定義します。いずれにせよ、これを管理することはできません。助けてください。Typescript:オブジェクトのタイプを定義する

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' 
    } 
}; 

答えて

1

あなたは非常に接近している、次のようになります。

const endPoints: { [name: string]: { method: string; url: string; } } = { 
    allFeed: { 
     method: 'GET', 
     url: 'https://www.yammer.com/api/v1/messages.json' 
    }, 
    ... 
}; 

あなたはまた、インタフェースを使用することができます。

interface EndPoint { 
    method: string; 
    url: string; 
} 

interface EndPointMap { 
    [name: string]: EndPoint; 
} 

const endPoints: EndPointMap = { 
    ... 
} 

または種類:

コードを作る
type EndPoint = { 
    method: string; 
    url: string; 
} 

type EndPointMap = { 
    [name: string]: EndPoint; 
} 

const endPoints: EndPointMap = { 
    ... 
} 

私の意見でもっと読みやすい(タイプを宣言するインラインの方法)

+0

ありがとうございました。 :-) –

関連する問題