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

Convert exported TypeScript types to undefined #10337

Closed
remcohaszing opened this issue Aug 14, 2019 · 6 comments · Fixed by #11171
Closed

Convert exported TypeScript types to undefined #10337

remcohaszing opened this issue Aug 14, 2019 · 6 comments · Fixed by #11171
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@remcohaszing
Copy link

Bug Report

Current Behavior

When the code below is compiled using webpack and babel-loader, this triggers the following warning:

WARNING in ./src/Bar/index.tsx 1:0-42
"export 'BarProps' was not found in './Bar'
 @ ./src/index.tsx

The original issue is here: babel/babel-loader#769

However, I believe it can be fixed upstream (in @babel/plugin-transform-typescript).

Input Code

src/Bar/Bar.tsx

export interface BarProps {
  value: string;
}

export default function Bar(props?: BarProps) {}

src/Bar/index.tsx

export { default, BarProps } from './Bar'

src/index.tsx (Webpack entry point)

import Bar from './Bar';

console.log(Bar())

Expected behavior/code

There are no warnings

Babel Configuration (.babelrc, package.json, cli command)

module.exports = {
  plugins: ['@babel/plugin-transform-typescript']
}

Environment

  • Babel version(s): 7.5.5
  • Node/npm version: Node 10/npm 6
  • OS: Ubuntu 18.04
  • Monorepo: no
  • How you are using Babel: loader

Possible Solution

Convert exported types and interfaces to undefined constants. This way, Webpack will find the constants and stop complaining and their types are still undefined when using typeof checks. A minifier should strip the constant, as it is unused in the final bundle.

I believe this should be an opt in solution on the Babel part. I.e. an option typeToConst: true for both @babel/plugin-transform-typescript and @babel/preset-typescript

Additional context/Screenshots

This webpack config can be used to verify behaviour.

module.exports = {
  devtool: 'source-map',
  resolve: {
    extensions: ['.tsx'],
  },
  module: {
    rules: [
      { 
        test: /\.tsx?$/, 
        use: 'babel-loader'
      }
    ]
  }
};
@babel-bot
Copy link
Collaborator

Hey @remcohaszing! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@realduolaf
Copy link

#10220 How's it going?

@JLHwung
Copy link
Contributor

JLHwung commented Dec 11, 2019

@realduolaf #10220 fixes flow type export. It does not address this issue.

@realduolaf
Copy link

@JLHwung OK~ Thank you.

@jackmellis
Copy link

Is there any more news on this issue? This actually breaks builds using rollup and I’m struggling to find a workaround that doesn’t involve abandoning the Babel plugin

@nicolo-ribaudo
Copy link
Member

In the next minor release, you'll be able to explicitly mark type-only exports using

export type { foo };

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 16, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants