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 declare variable with the same name as TypeScript type import #10015

Open
leethree opened this issue May 23, 2019 · 5 comments

Comments

Projects
None yet
4 participants
@leethree
Copy link

commented May 23, 2019

Bug Report

Current Behavior
Babel shows error "Identifier 'xxx' has already been declared" when variable and type uses the same name.

Input Code

import React, { Context } from 'react';

const Context: Context<{}> = React.createContext({});

export { Context };

Error message

/repl: Identifier 'Context' has already been declared (3:6)

Expected behavior/code
Types should be ignored by Babel and name conflict should not happen. This code is acceptable by TypeScript compiler even with isolatedModules option.

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

{
  presets: ['module:metro-react-native-babel-preset']
}

Environment

  • Babel version(s): v7.4.5
  • Node/npm version: v10.15.0
  • OS: macOS 10.14.5
  • Monorepo: no
  • How you are using Babel: React Native metro bundler

Possible Solution
It can be inferred from the file that the imported identifier is a type. So it should not count as an identifier.

Additional context
Related issue: #7943

It's very common in TypeScript to use the same identifier for variable and type without introducing a name conflict. This behaviour should be supported by Babel as well.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented May 23, 2019

Hey @leethree! 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.

@leethree leethree changed the title Can't declare variable with the same name as TypeScript type impport Can't declare variable with the same name as TypeScript type import May 23, 2019

@dangreen

This comment has been minimized.

Copy link

commented May 28, 2019

(almost) Same thing:

type WeatherState = ReturnType<Record.Factory<IWeatherStateProps>>;

const WeatherState = Record<IWeatherStateProps>({
	city:            null,
	currentWeather:  null,
	weatherForecast: List()
});

export {
	WeatherState
};
...
import { WeatherState } from '...';
...
WeatherState();

throws:

TypeError: (0 , _Weather.WeatherState) is not a function
@dangreen

This comment has been minimized.

Copy link

commented May 28, 2019

In my case, @babel/plugin-transform-typescript@7.4.0 solves problem (need to check lock file, to be sure we have't other versions)

@Jessidhia

This comment has been minimized.

Copy link
Member

commented Jun 5, 2019

The REPL is a bit annoying to check but yes, this is affecting code on actual builds.

@leethree

This comment has been minimized.

Copy link
Author

commented Jul 9, 2019

@babel/plugin-transform-typescript@7.4.0 doesn't solve the problem for me. Also we need higher version to resolve other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.