2016-12-14 8 views
0

に他のオブジェクト型からなるオブジェクトを定義する:は、私はこのようになりますオブジェクトを持つフロー

const myObject = { 
    a: { 
    id: 'abc' 
    }, 
    b: { 
    id: 'def' 
    }, 
    c: { 
    id: 'ghi' 
    }, 
} 

、B、Cのすべてが同じ構造を持っているので、私は、単一の型を定義したいと思いますそれらのもの(以下のabcType)を参照し、myObjectがこれらのタイプから構成されていることを確認してください。そこで、基本的

type abcType = { 
    id: String 
} 

type myObjectType = { 
    [whateverKey]: abcType 
    // ^^ Dummy code, this doesn’t actually work 
} 

しかし、私はフローでこれを定義するための適切な方法を見つけるように見えることはできません。同じ問題を抱えている人は誰ですか?

答えて

1
type abcType = { 
    id: string // lower-case 's' 
} 

type myObjectType = { 
    [string]: abcType // 'string' type for the object keys. 
}; 

const myObject: myObjectType = { 
    a: { 
    id: 'abc' 
    }, 
    b: { 
    id: 'def' 
    }, 
    c: { 
    id: 'ghi' 
    }, 
} 

すべてが必要です。

+0

大変ありがとうございます! – Mattijs

関連する問題