facebook / facebook/flow

[Question] Use external constant when defining flow literal type

Open
#4,279 12 comments 6 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 would like to use an imported constant from a package in a flow literal type and static check the switch.

Example:

```javascript
// action/types.js
import { REHYDRATE } from 'redux-persist/constants'

export type FooBar = {
foo: number,
bar: string,
};

export type Action
= { type: 'FETCH_REQUEST', data: FooBar[] }
| { type: REHYDRATE, payload: any } // <== this do not work
;

// reducer.js
import { REHYDRATE } from 'redux-persist/constants'

export default function (state: State = initialState, action: Action)
switch (action.type) {
case 'FETCH_REQUEST':
// do something with action.data
case REHYDRATE: { // <= flow says: uncovered code
// do something with action.payload
default:
return state
}
}
```

Related StackOverflow question: https://stackoverflow.com/q/44834435/1715004

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.