Skip to content

Failure to use typeof of imports in unions #3229

@joaosoares

Description

@joaosoares

Hello,

I am having a problem when creating unions that use typeof to determine the type of imported variables. So, for example, the following doesn't work:

Example that doesn't work

constants.js

export const PRODUCT_SET_ID: 'PRODUCT_SET_ID' = "PRODUCT_SET_ID"
export const PRODUCT_RESET: 'PRODUCT_RESET' = "PRODUCT_RESET"

actions.js

import {
  PRODUCT_SET_ID,
  PRODUCT_RESET
} from './constants'

export type Action =
  {| type: typeof PRODUCT_SET_ID, id: number |} |
  {| type: typeof PRODUCT_RESET, id: number |}

export const productSetId = (id: number) : Action => ({
  type: PRODUCT_SET_ID,
  id,
})

This returns the error object literal Could not decide which case to select union type Case 1 may work: exact type: object type But if it doesn't, case 2 looks promising too: exact type: object type Please provide additional annotation(s) to determine whether case 1 works (or consider merging it with case 2): identifier 'PRODUCT_SET_ID'

Works without import

If I substitute the imports for inline declarations, the whole example works.

actions.js

export const PRODUCT_SET_ID: 'PRODUCT_SET_ID' = "PRODUCT_SET_ID"
export const PRODUCT_RESET: 'PRODUCT_RESET' = "PRODUCT_RESET"

export type Action =
  {| type: typeof PRODUCT_SET_ID, id: number |} |
  {| type: typeof PRODUCT_RESET, id: number |}

export const productSetId = (id: number) : Action => ({
  type: PRODUCT_SET_ID,
  id,
})

I don't think this should happen, as it makes it harder to separate code into modular files. Any solutions or workarounds to continue using import?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions