Flow: Regression: union of string literals can't be used as computed properties

Created on 14 Jan 2020  路  4Comments  路  Source: facebook/flow

Flow version: v0.111 and up have this regression. v0.110 and lower work fine

Expected behavior

Flow doesn't report an error.

Actual behavior

Flow reports an error for the following code:

// @flow
const key: 'a' | 'b' = 'a';
const obj = {[key]: 42};

Error:

    3: const obj = {[key]: 42};
                     ^ Cannot use `key` [1] as a computed property. Computed properties may only be primitive literal values, but union type [2] is a union. Can you add a literal type annotation to `key` [1]? See https://flow.org/en/docs/types/literals/ for more information on literal types.
        References:
        2: const key: 'a' | 'b' = 'a';
                 ^ [1]
        2: const key: 'a' | 'b' = 'a';
                      ^ [2]
not a bug

Most helpful comment

Thanks for pointing me to this article. From reading the section on unions in computed properties, it sounds like this decision was made for performance considerations in a contrived scenario, and not because the code is actually faulty. Therefore, I'd disagree with the classification as "not a bug", since Flow is now complaining about valid code that was working prior to this change.

All 4 comments

This is deliberate as part of the changes described here: https://medium.com/flow-type/spreads-common-errors-fixes-9701012e9d58

Thanks for pointing me to this article. From reading the section on unions in computed properties, it sounds like this decision was made for performance considerations in a contrived scenario, and not because the code is actually faulty. Therefore, I'd disagree with the classification as "not a bug", since Flow is now complaining about valid code that was working prior to this change.

It's not a contrived scenario-- it makes Flow time out in our codebases.

I wonder who has 10k members sum types ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

STRML picture STRML  路  48Comments

blax picture blax  路  83Comments

opensrcery picture opensrcery  路  88Comments

TylerEich picture TylerEich  路  49Comments

gcanti picture gcanti  路  48Comments