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.

preset-typescript option "onlyRemoveTypeImports" is not working

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current behavior

Input Code I am trying transpile ts with babel using @babel/preset-typescript, but one of the challange I am facing is that, when injecting classes as parameter, import assigned type is removed by preset-typescript. I then came accross "onlyRemoveTypeImports": true which promises to do exactly what I am trying to achieve. However, this does not seem to work 😦

Here is my ts file

some-service.ts

// THIS SHALL NOT BE REMOVED
import AWS from 'aws-sdk';

@Injectable()
export class SomeService {

 constructor(
    @Inject(AWS_TOKEN.DOCUMENT_CLIENT) private dc: AWS.DynamoDB.DocumentClient,
  ) {}

// ... do some stuff with this.dc 
}

But in final output AWS import is removed as it wan’t used within a service. and running transpiled file gives me "ReferenceError: AWS is not defined" error

Expected behavior when onlyRemoveTypeImports option is given, typescript preset should only ever remove type imports as stated in documentation.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: .babelrc
{
  "presets": [
    [
      "@babel/preset-env"
    ],
    [
      "@babel/preset-typescript",
      {
        "onlyRemoveTypeImports": true
      }
    ]
  ],
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    ["@babel/plugin-proposal-class-properties"]
  ]
}

Environment

  System:
    OS: Linux 4.15 elementary OS 5.1.3 Hera
  Binaries:
    Node: 12.5.0 - ~/.nvm/versions/node/v12.5.0/bin/node
    npm: 6.13.7 - ~/.nvm/versions/node/v12.5.0/bin/npm
  npmPackages:
    @babel/plugin-proposal-class-properties: ^7.10.4 => 7.10.4 
    @babel/plugin-proposal-decorators: ^7.10.5 => 7.10.5 
    @babel/preset-env: ^7.11.5 => 7.11.5 
    @babel/preset-typescript: ^7.9.0 => 7.10.4 
    babel-plugin-transform-typescript-metadata: ^0.3.0 => 0.3.0 

  • Babel version(s): [ v6.26.0]
  • How you are using Babel: [cli]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
existentialismcommented, Oct 7, 2020

@whimzyLive can you create a repo that reproduces the problem? the import isn’t removed when I test this locally.

0reactions
whimzyLivecommented, Oct 11, 2020

This indeed was a issue with babel-plugin-transform-typescript-metadata.

You can read more about it here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - TypeScript 3.8
import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of...
Read more >
Do I need to use the "import type" feature of TypeScript 3.8 if ...
Short answer: Being more explicit by using import type and export type statements seem to yield explicable benefits by safeguarding against ...
Read more >
type-only imports — A new TypeScript feature that benefits ...
Issues arise when Babel can't possibly know if a particular import is a type that should be removed or an actual value that...
Read more >
babel/preset-typescript
Replace the function used when compiling JSX expressions. This is so that we know that the import is not a type import, and...
Read more >
consistent-type-imports | typescript-eslint
This option defines the expected import kind for type-only imports. ... It is the default. no-type-imports will enforce that you always use import...
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