facebook / facebook/flow

How to get the union of the specific fields of another union ?

Aperta
#3,373 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I'm trying to have as much type safety as possible on my Redux app and I'm stuck with this problem. Basically I have these types:

type ActionA = { type: 'A' }
type ActionB = { type: 'B' }

type Actions = A | B

And I'd like to have a type `ActionType` that is the union of all the possible `type`s of my actions. i.e:

type ActionType = 'A' | 'B' // <-- How to get this from `Actions` ?

In other words, I would like to get the union of all the possible values of a field (`type`) in an union (`A | B`).

Is this even possible with flow ?

The use case is to be able to avoid misspelling actions types when I need to write them directly. For example, using `redux-saga`, I could write a definition of `take` that would be (over-simplified for the example):

define function take(action: ActionType, saga: Function)

Then I would be sure I can only call `take` with valid action types.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.