Giter Site home page Giter Site logo

Comments (11)

leonardfactory avatar leonardfactory commented on June 29, 2024 1

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.

leonardfactory avatar leonardfactory commented on June 29, 2024

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.

kelvien avatar kelvien commented on June 29, 2024

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.

leonardfactory avatar leonardfactory commented on June 29, 2024

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.

kelvien avatar kelvien commented on June 29, 2024

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.

kelvien avatar kelvien commented on June 29, 2024

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.

kelvien avatar kelvien commented on June 29, 2024

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.

mmahalwy avatar mmahalwy commented on June 29, 2024

@kelvien any luck with this? I see you're using nextjs too

from babel-plugin-transform-typescript-metadata.

kelvien avatar kelvien commented on June 29, 2024

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.

mmahalwy avatar mmahalwy commented on June 29, 2024

@kelvien oh yeah, but that could get really messy really quickly 😓

from babel-plugin-transform-typescript-metadata.

MatthieuCoelho avatar MatthieuCoelho commented on June 29, 2024

Got the same issue with nextjs, still no solution ? =(

from babel-plugin-transform-typescript-metadata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.