2017-03-11 1 views
0

Ich habe eine Baumstruktur wie unten definiert. Die options -Eigenschaft ist entweder ein Array von ParentActions oder ein Array von Optionen. Aber ich bin mir nicht sicher, wie ich den Flow Typechecker dazu bringen soll, das korrekt zu überprüfen.Flow Javascript typecheck eine rekursive Definition mit Union von Arrays eines einzelnen Elementtyps

// @flow 
type Option = { 
    label: string, 
    action: string, 
    type: 'leaf' 
} 

type ParentActions = { 
    prompt: string, 
    depth: number, 
    label: string, 
    options: Array<ParentActions> | Array<Option>, 
    type: 'parent' 
} 

type RootActions = { 
    prompt: string, 
    options: Array<ParentActions> | Array<Option>, 
    type: 'root' 
} 

const thisDoesNotTypeCheckCorrectly : RootActions = { 
    options: [ 
    { 
     label: 'string', 
     action: 'string', 
     type: 'leaf' 
    }, 
    { 
     label: 'string', 
     action: 'string', 
     type: 'leaf' 
    } 
    ], 
    type: 'root' 
}; 
/* Could not decide which case to select 
union type: test.js: 15 
Case 1 may work: 
array type: test.js: 15 
But if it doesn't, case 2 looks promising too: 
array type: test.js: 15 
Please provide additional annotation(s) to determine whether case 1 works (or consider merging it with case 2): 
inferred union of array element types (alternatively, provide an annotation to summarize the array element type) */ 

Antwort

0

Versuchen Sie folgendes:

type RootActions = { 
    prompt: string, 
    type: 'root', 
} & ({ options: Array<ParentAction> } | { options: Array<Option> }) 
Verwandte Themen