facebook / facebook/flow

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

Open
#3,373 2 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.