2017-06-13 4 views
0

verschachtelte Objekte in Strömungsart

export type TOption = { 
 
    input_type: string, 
 
    label: string, 
 
    option_id: string, 
 
    value: any 
 
}; 
 

 
export type TOptions = { 
 
    [key: string]: TOption 
 
};

Ich versuche, einen Typen zu definieren, die ein Objekt ist, das eine dynamische Anzahl von Schlüssel-Wert-Paaren aufweist, wobei jeder Wert ein Objekt mit INPUT_TYPE, Etikett, option_id, Wert

jedoch Fluss wirft nicht kompatiblen Typ Fehler

Property `option_id` is incompatible: 
 
    19: [key: string]: TOption 
 
          ^^^^^^^ object type. This type is incompatible with 
 
    14: option_id: string, 
 
         ^^^^^^ string

Ich denke, dass ich es falsch bin definieren, aber ich bin nicht sicher, wie es zu definieren. Kann jemand auf das Problem hinweisen?

Antwort

1

Der folgende Code funktioniert für dynamische verschachtelte Typen. Wenn Sie versuchen, dasselbe zu erreichen.

/* @flow */ 

type TOption = { 
    input_type: string, 
    label: string, 
    option_id: string, 
    value: any 
} 

type TOptions = { 
    [key: string]: TOption 
} 

var myObj: TOptions = { 
    data: { 
     input_type: 'my_input_type', 
     label: 'my_label', 
     option_id: 'my_option_id', 
     value: 12 
    } 
} 
+0

interessant. Ich muss weiter darauf schauen, warum meine Version mit Flow ausfällt – Victor

Verwandte Themen