2017-05-15 3 views
1

Ich versuche, Flow mit einer Redux-Codebasis zu integrieren.
Ich bin ziemlich neu in Flow, aber ich habe schon ein bisschen mit TypeScript gespielt.Korrekte Eingabe des Prüfdruckminderers mit Durchfluss

Ich möchte in der Reducer falschen Aktionstyp zu fangen.

type Action = 
    | { type: 'sample' } 
    | { type: 'django' } 
    ; 

type State = { 
    content: string, 
}; 

const reducer = (state: State, action: Action): State => { 
    switch (action.type) { 
    // OK 
    case 'sample': 
     return { content: '' }; 

    // Should raise a type error, because the action type 
    // will never be equal to "error" 
    case 'error': 
     return { content: '' }; 

    default: 
     return { content: '' }; 
    } 
}; 

Exemple in Try Flow
Exemple in TypeScript Playground

Ich verstehe nicht, warum der Fluss nicht den Fehler in diesem Fall fangen. Flow folge die type Eigenschaft als string, aber ich legte explizit den Typ auf .

Habe ich etwas übersehen?

Danke!

Antwort

1

Dies scheint ein Fehler in Fluss, aber Sie können eine strenge Überprüfung erzwingen wie folgt:

type ActionType = 'sample' | 'django' 
type Action = {type: ActionType} 

type State = { 
    content: string, 
}; 

const reducer = (state: State, action: Action): State => { 
    const type:ActionType = action.type; 
    switch (type) { 
    // Should raise a type error, because the action type 
    // will never be equal to "error" 
    case 'error': 
     return { content: '' }; 
    default: 
     return { content: '' }; 
    } 
}; 

Dies gibt:

13:  case 'error': 
      ^string literal `error`. This type is incompatible with 
9: const type:ActionType = action.type; 
       ^string enum 
+1

in der Tat einen Fehler/work in progress - siehe https://github.com/facebook/flow/issues/2399 –

Verwandte Themen