Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type spread on imported types doesn't work #230

Open
2 of 10 tasks
konstantinkreft opened this issue Jul 11, 2019 · 3 comments
Open
2 of 10 tasks

Type spread on imported types doesn't work #230

konstantinkreft opened this issue Jul 11, 2019 · 3 comments

Comments

@konstantinkreft
Copy link

This is a:

  • Bug Report
  • Feature Request
  • Question
  • Other

Which concerns:

  • flow-runtime
  • babel-plugin-flow-runtime
  • flow-runtime-validators
  • flow-runtime-mobx
  • flow-config-parser
  • The documentation website

What is the current behaviour?

Example:

import type { Bar } from './types';

type Foo = {|
  ...Bar,
  foobar: string,
|};

If a type is imported from external file and spread into another type the following error is thrown: TypeError: Invalid attempt to spread non-iterable instance.

It seems like that at this point the @babel/flow preset already stripped out the types so it tries to spread undefined.

But if both types are declared in the same file everything works fine.

To showcase this bug I created a minimal reproducible example over here: https://github.com/risetechnologies/flow-runtime-bug


What is the expected behaviour?

It should be possible to spread a type from external file into another type.


Which package versions are you using?

flow-runtime: 0.17.0
babel-plugin-flow-runtime: 0.19.0
@tebuevd
Copy link

tebuevd commented Mar 26, 2020

any work around for this?

@gajus
Copy link
Owner

gajus commented Mar 27, 2020

If anyone contributes a fix, I will make sure it gets released.

@jedwards1211
Copy link
Collaborator

I might look into this soon if I have time...

It seems like that at this point the @babel/flow preset already stripped out the types so it tries to spread undefined

I don't think type stripping would cause any problems like this? Because flow-runtime converts all type imports/exports to runtime values. If types get stripped before babel-plugin-flow-runtime, then everything breaks because nothing in babel-plugin-flow-runtime can function without type annotations.

I forget the specifics of how presets interact with plugin order, but in my .babelrc.js I do explicitly specify the order of the strip types transform:

{
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    [
      'babel-plugin-flow-runtime',
      { assert: false, annotate: false, optInOnly: true },
    ],
    '@babel/plugin-proposal-async-generator-functions',
    '@babel/plugin-proposal-object-rest-spread',
    '@babel/plugin-proposal-optional-chaining',
    '@babel/plugin-proposal-nullish-coalescing-operator',
    'babel-plugin-dynamic-import-node',
    '@babel/plugin-transform-flow-strip-types',
    ['@babel/plugin-proposal-class-properties', { loose: true }],
    'babel-plugin-lodash',
    'babel-plugin-dedent',
    '@babel/plugin-transform-runtime',
  ],
  presets: [
    ['@babel/preset-env', { targets: { node: 'current' } }],
    '@babel/preset-react',
    '@babel/preset-flow',
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants