@babel/typescript removed types which prevents decorator metadata from being emitted
See original GitHub issueBug 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:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Oh ok. Babel doesn’t load the
tsconfig.json
file, but it transforms TS as if you were transforming it only with theisolatedModules: true
option.I suggest that you use something like this to define those metadata:
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