facebook / facebook/flow

Any way to acknowledge "constant contexts" (e.g. string literal concatenation)?

Open
#5,460 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Use case: I have a function which takes 2 strings and returns the concatenated result, joined by a slash:

```flow
export const createScopedActionType: CurriedFunction2 = _.curry((scope: string, type: string): string => `${scope}/${type}`);
```

I use this function to create scoped Redux action types like this:
```js
const createActionType = createScopedActionType("NavigationBlock");

const ADD_NAVIGATION_BLOCK = createActionType("ADD_NAVIGATION_BLOCK");
console.log(ADD_NAVIGATION_BLOCK) // NavigationBlock/ADD_NAVIGATION_BLOCK

const REMOVE_NAVIGATION_BLOCK = createActionType("REMOVE_NAVIGATION_BLOCK");
console.log(REMOVE_NAVIGATION_BLOCK ) // NavigationBlock/REMOVE_NAVIGATION_BLOCK
```

Obviously, when I call my function with 2 string literals, the result is already determined at compile time.
According to the docs it's recommended to define my actual action object types like this:
```js
type AddAction = { type: _string_literal_, ...payload };
```

However, when using my defined action types to define the type property, Flow throws an error since I'm using an ineligible value, even though the value is defined after parsing and simple evaluation in practice.

Is there any existing way to solve this for my particular use case?

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.