question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript: enum member with the same name as an imported type prevents import stripping

See original GitHub issue

Bug Report

Current Behavior If the name of an enum member matches that of an imported type, the corresponding import statement remains through transpilation, even if it’s never referenced.

Input Code

import Foo from 'some-module';

enum Enum {
  Foo
}

function myFunction(): Foo {}

(REPL link)

Expected behavior/code I expect not to see import Foo from 'some-module'; in the compiled output, since Foo is only ever used as a type.

Note that changing the name of the enum member, e.g. to Foo2, causes the import statement to be stripped.

Environment

  • @babel/core: 7.1.2
  • @babel/plugin-transform-typescript: 7.1.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
chriskrychocommented, Oct 25, 2018

@dfreeman I assume changing the name of the import also fixes the problem, i.e. with this?

import { Foo as Foo2 } from './foo';

enum Enum {
  Foo
}

function myFunction(): Foo2 {}
1reaction
nicolo-ribaudocommented, Feb 10, 2019

This is a real bug. In this example the no-unused-variable rule wouldn’t warn:

import Foo from 'some-module';

enum Enum {
  Foo
}

function myFunction(): Foo { }

export default Enum.Foo;
Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - How to import an Enum - Stack Overflow
I'm using global TS importing and TS can't see the exported enum that I have put in a different file for this very...
Read more >
Handbook - Enums - TypeScript
String enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member...
Read more >
TypeScript: Don't Export const enums - ncjamieson
the compiler reads a TypeScript module;; the module's type information is stripped; and; what's left is the JavaScript module that the compiler ...
Read more >
TypeScript Fundamentals - Joy of Code
Your editor already has some great features that get enhanced by TypeScript: Auto imports (as you type imports get added); Code navigation (definitions, ......
Read more >
48 answers on StackOverflow to the most popular Angular ...
Importing lodash into angular2 + typescript application ... Only the value false or removing the attribute will actually make the element ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found