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

Can't use named exports for TypeScript types? #6570

Closed
zaynv opened this issue Mar 4, 2019 · 5 comments
Closed

Can't use named exports for TypeScript types? #6570

zaynv opened this issue Mar 4, 2019 · 5 comments

Comments

@zaynv
Copy link

zaynv commented Mar 4, 2019

Is this a bug report?

Yes

Did you try recovering your dependencies?

Not relevant.

Which terms did you search for in User Guide?

TypeScript

Environment

System:
OS: macOS 10.14.2
CPU: x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
Binaries:
Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
Yarn: 1.5.1 - /usr/local/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
Browsers:
Chrome: 72.0.3626.119
Firefox: 65.0
Safari: 12.0.2
npmPackages:
react: ^16.8.3 => 16.8.3
react-dom: ^16.8.3 => 16.8.3
react-scripts: 2.1.5 => 2.1.5
npmGlobalPackages:
create-react-app: 2.1.5

Steps to Reproduce

  1. create-react-app --typescript example
  2. Create a file in src called types.ts
  3. Add the following code to types.ts:
interface Person {
  name: string
  age: number
}

export { Person }

Expected Behavior

This should be fine.

Actual Behavior

Failed to compile.

/Users/zaynv/Projects/misc/example/src/types.ts
Type error: Cannot re-export a type when the '--isolatedModules' flag is provided.  TS1205

    4 | }
    5 |
  > 6 | export { Person }
      |          ^
    7 |

I can't remove the --isolatedModules flag because create-react-app seems to put it back in automatically when it compiles. Is there really no way to fix this issue other than having to use export interface X and export interface Y everywhere instead of export { X, Y }?

Reproducible Demo

I can make one if necessary, but its basically just a new app with an exported type.

@heyimalex
Copy link
Contributor

You can check through the tracker for other issues about this to learn more about the why, but I think the short answer is that it's a limitation of using babel to compile typescript, and so yeah you are limited by whatever restrictions --isolatedModules forces.

@zaynv
Copy link
Author

zaynv commented Mar 4, 2019

Hmm, do you think this could be better documented in the create-react-app typescript docs? One of the notes say:

You are allowed to edit the generated TypeScript configuration.

Which doesn't exactly seem to be the case as the isolatedModules: true seems to be enforced in the config?

Also, it mentions:

Note: Constant enums and namespaces are not supported.

But I guess the export style I mentioned is another limitation. I can't be the only one who uses the export { Type } style at the bottom of the file over export interface Type {}, so do you all think it might be a good idea to mention it there?

@ianschmitz
Copy link
Contributor

ianschmitz commented Mar 9, 2019

Have you tried export interface Person { ... } instead?

Here's some related info: microsoft/TypeScript#21194

@zaynv
Copy link
Author

zaynv commented Mar 9, 2019

@ianschmitz Yes that works, I mentioned in the first comment that this way would work. I just think it's problematic that a certain style of coding is not allowed and is not mentioned anywhere

@ianschmitz
Copy link
Contributor

There's not much we can do on our end. This is expected behavior from TypeScript with isolatedModules enabled. Unfortunately we need to keep isolatedModules enabled for compatibility with babel 7. Here is some more background info: https://devblogs.microsoft.com/typescript/typescript-and-babel-7/

@lock lock bot locked and limited conversation to collaborators Mar 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants