2017-08-07 11 views
0

私はapollo-codegenを使用して、私のプロジェクトのtsxファイルに埋め込まれたGraphQLクエリからTypeScript型を生成しています。生成されたタイプの例は:Typescript:名前付きの型の子である匿名型を参照するにはどうすればよいですか?

export type MooQuery = { 
    wines: { 
    edges: Array< { 
     node: { 
     nodeId: string, 
     wineName: string | null, 
     wineryId: number | null, 
     vintageId: number | null, 
     uomId: number | null 
     }, 
    } | null > | null, 
    } | null, 
}; 

Iは、各ノード反応成分をレンダリングするヘルパーメソッドに「ノード」を渡すことになります。私は次のようなことをしたいと思っています:

renderHelper(node: MooQuery.wines.edges.node) { // this won't work 

} 

私が欲しいものを達成する方法はありますか?私が望んでいることは理にかなっていますか?

答えて

1

あなたのケースで、私はちょうどタイプを編集したいが、あなたはブラケット記法

renderHelper(node: MooQuery['wines']['edges'][0]['node']) { // this should work 
    //           ^^^ Don't forget the 0 for the array 
} 

を使用することができます(あなたが出始めて一度だけ、それを生成し、私は願っています?)

export type MooQuery = { 
    wines: { 
    edges: Array<{node: MooNode} | null > | null, 
    } | null, 
}; 

export type MooNode = { 
    nodeId: string, 
    wineName: string | null, 
    wineryId: number | null, 
    vintageId: number | null, 
    uomId: number | null 
} 

その後ただMooNode

+0

これはすばらしい、ありがとう!しかし、私は非常に奇妙なエラーが発生します: 'エラーTS2339:プロパティ 'edges'はタイプ '{edges:({node:{nodeId:string; wineName:string | null; wineryId:number | null; vintageId:..私には矛盾しているように見えますか? –

+0

私はそれを再現できません。新しい質問を開き、これにリンクすることをお勧めします。 –

+0

ありがとうございます:) –

関連する問題