Skip to content

Convert exported TypeScript types to undefined #10337

@remcohaszing

Description

@remcohaszing

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'
      }
    ]
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: typescriptoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions