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.

@babel/typescript removed types which prevents decorator metadata from being emitted

See original GitHub issue

Bug Report

Current Behavior It logs couldn't get the type :(.

Input Code

import "reflect-metadata";

const logType = (target: any, key: string): void => {
  const type = Reflect.getMetadata("design:type", target, key);
  if (!type) {
    console.log("couldn't get the type :(");
    return;
  }
  console.log(`${key} type: ${type.name}`);
};

class Demo {
  @logType
  public someProperty: number = 0;
}

new Demo();

Expected behavior/code It should log someProperty type: Number.

Babel Configuration (.babelrc)

{
  "presets": ["@babel/env", "@babel/typescript"],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true }],
    "@babel/proposal-class-properties",
    "@babel/proposal-object-rest-spread"
  ]
}

Environment

  • Babel version(s): 7.2.0
  • Node/npm version: doesn’t matter
  • OS: doesn’t matter
  • Monorepo: no
  • How you are using Babel: babel-node/cli

Possible Solution There is a package babel-decorators-metadata which tries to solve this issue but at the same time breaks some stuff. There is also a question on stackoverflow regarding this issue.

Additional context/Screenshots @babel/typescript removes TypeScript type definitions before compilation process which causes packages such as reflect-metadata to not work correctly.

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
nicolo-ribaudocommented, Mar 14, 2019

Because there is an emitDecoratorMetadata set to true in the tsconfig.json

Oh ok. Babel doesn’t load the tsconfig.json file, but it transforms TS as if you were transforming it only with the isolatedModules: true option.

I suggest that you use something like this to define those metadata:

import "reflect-metadata";

const logType = (target: any, key: string): void => {
  const type = Reflect.getMetadata("design:type", target, key);
  if (!type) {
    console.log("couldn't get the type :(");
    return;
  }
  console.log(`${key} type: ${type.name}`);
};

class Demo {
  @logType
  @Reflect.metadata("design:type", Number)
  public someProperty: number = 0;
}

new Demo();
3reactions
leonardfactorycommented, Mar 25, 2019

Hi, I developed a plugin in order to emit decorators metadata, similar to TSC compiler, even if using babel with @babel/preset-typescript: babel-plugin-transform-typescript-metadata

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Decorators - TypeScript
Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.
Read more >
Babel vs. TypeScript: Choosing the right compiler for your ...
There's one other TypeScript decorators feature we should talk about: emitDecoratorMetadata . TypeScript normally erases all type information so ...
Read more >
Typescript Decorator and Babel: ColumnTypeUndefinedError ...
Despite having babel-plugin-transform-typescript-metadata installed I have this error below: ColumnTypeUndefinedError: Column type for ...
Read more >
TypeScript With Babel: A Beautiful Marriage - I Am Turns
How can Babel handle the TypeScript type checking? TypeScript can already output to ES5 just like Babel can, so what's the point? Isn't...
Read more >
Understanding TypeScript Configuration Options
When noEmit is true , TypeScript will not emit outputs. You can use it when you want TypeScript to do only type checking,...
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