Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic property names in flow typed object

I'm slowly and surely working flowType into my code, but I'm struggling with one concept.

How do I specify the type of unknown, dynamically named properties of an object?

For example my user object might have an object containing organisations with unique keys.

How would I define this?

export type User = ?{   currentOrg: string,   displayName?: string,   email: string,   emailVerified: boolean,   newAccount: boolean,   organisations?: {      UNKNOWNKEY?: string {        orgData1: string,        orgData2: string,      }   },   uid: string,   photoUrl?: string, }; 

Would really appreciate any help on this. Thanks!

like image 840
Sam Matthews Avatar asked Feb 11 '17 09:02

Sam Matthews


1 Answers

Flow has specific syntax for objects that behave like maps:

{ [key: K]: V } 

where K is they type of the keys and V is the type of the values.

Your full example would look like:

export type User = {   currentOrg: string,   displayName?: string,   email: string,   emailVerified: boolean,   newAccount: boolean,   organisations?: { [key: string]: string },   uid: string,   photoUrl?: string, }; 
like image 198
Peter Hall Avatar answered Sep 30 '22 05:09

Peter Hall