Comments (11)
Probably TypeScript is smarter here while generating decorators and is wrapping them in functions. I'll see if this is solvable using this plugin alone, or if the problem arises from babel decorators generation process
from babel-plugin-transform-typescript-metadata.
Usually in TypeORM you can simply use @OneToOne(type => Entity)
instead of @OneToOne(Entity)
to resolve the circular dependency which is unsupported directly in JS. Have you tried this way?
from babel-plugin-transform-typescript-metadata.
Do you mean passing the string instead of the function, right? Suggested here - typeorm/typeorm#4190
I tried that too and it didn't work. Here is the generated code:
_dec4 = Object(typeorm["OneToOne"])('User'), _dec5 = Reflect.metadata("design:type", typeof User === "undefined" ? Object : User)
The relationship is referenced by string, but the Reflect.metadata is doing that typeof
check with the class that does not exist yet.
from babel-plugin-transform-typescript-metadata.
Didn't know about the string version, however the problem here is in the type definition (user: User;
) not the relationship definition (@OneToOne('User', 'user')
)
I may suggest to type user: User;
as user: User | undefined;
. This should relax the type definition
from babel-plugin-transform-typescript-metadata.
I gave that a try, but the generated code doesn't change at all
Code:
@OneToOne('Profile')
profile?: Profile | undefined;
Transpiled code
_dec9 = Reflect.metadata("design:type", typeof Profile === "undefined" ? Object : Profile
from babel-plugin-transform-typescript-metadata.
Changing it to Profile | null
also generate the same code.
But profile?: Profile | string
results into _dec26 = Reflect.metadata("design:type", Object)
, obviously not what I wanted but at least has less stricter type check
from babel-plugin-transform-typescript-metadata.
Update: I also notice that my Babel/Webpack is outputting a different result between dev and prod. And it seems like it works fine if Webpack bundles my barrel file correctly (a rollup index file for all of my db models). I put more details of my question here - https://stackoverflow.com/questions/65102724/inconsistent-webpack-with-babel-output-in-using-a-barrel-file-between-dev-and-pr
from babel-plugin-transform-typescript-metadata.
@kelvien any luck with this? I see you're using nextjs too
from babel-plugin-transform-typescript-metadata.
Hey @mmahalwy, one alternative was to put the models with circular dependency into one file, it's not the most ideal approach, but I can't seem to find a better solution other than fixing the transpiler itself.
from babel-plugin-transform-typescript-metadata.
@kelvien oh yeah, but that could get really messy really quickly 😓
from babel-plugin-transform-typescript-metadata.
Got the same issue with nextjs, still no solution ? =(
from babel-plugin-transform-typescript-metadata.
Related Issues (20)
- Support new decorators proposal HOT 1
- Adding this plugin to the plugin list breaks other Babel functionality HOT 7
- missing class metadata in parameter-decorated classes HOT 2
- order of applied class/constr-param decorators different than in tsc HOT 5
- Class method does not emit 'design:returntype' and 'design:type' HOT 1
- [survey] TypeScript and Babel feedback needed HOT 4
- Redefine type always emit Object design:type HOT 5
- Unexpected token problem HOT 3
- ReferenceError occurs when referencing a class before it's defined HOT 3
- Is it possible to get that into a create-react-app? HOT 2
- Reference Error "X is not defined" HOT 9
- Error when import some type from another package HOT 6
- Example usage with babel standalone HOT 1
- Enums types with propery decorators are incorrectly transformed HOT 2
- Add info about resolving bundler warnings to README HOT 3
- Add a babel preset with this enabled HOT 2
- design:paramtypes does not work with object assignment HOT 1
- ColumnTypeUndefinedError: Column type for Exercise#name is not defined and cannot be guessed. HOT 2
- Add an option to disable the translation of parameter decorators HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from babel-plugin-transform-typescript-metadata.