Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support legacy TS decorators in properties without initializer #367

Open
octet-stream opened this issue Feb 17, 2025 · 1 comment
Open
Labels
bug Something isn't working

Comments

@octet-stream
Copy link

octet-stream commented Feb 17, 2025

Environment

Jiti: 2.4.2
Node.js: v23.7.0

Reproduction

https://github.com/octet-stream/jiti-class-properties-issue

Describe the bug

I've been working on Mikro ORM integration with jiti, so we can replace ts-node for transpilation. I decided to add jiti (among other options), because as far as I can tell - it is more compatible with tsc comparing to tsx, and this is very critical, because Mikro ORM relies on legacy decorators and emitDecoratorsMetadata as one of the options to define entities.

However, jiti fails to run code with TS legacy decorators on properties if they're not initialized (even in constructor).

Here's how typical Mikro ORM entity looks like:

import {Entity, PrimaryKey, Property} from "@mikro-orm/core"

@Entity()
export class User {
 @PrimaryKey()
 id!: number

 @Property()
 fullName!: string

 @Property()
 email!: string

 @Property()
 password!: string

 @Property({type: "text"})
 bio = ""
}

This code is valid for TS and other solutions I've been using so far. But if you import it via jiti, or run with jiti/register, you'll see following error:

node:internal/modules/run_main:104
    triggerUncaughtException(
    ^

[Error: TRANSFORM_ERROR: Definitely assigned fields cannot be initialized here, but only in the constructor
 jiti-class-properties-issue/issue.ts:0:0]

Node.js v23.7.0

Related #57
This could be the fix: #361

Additional context

No response

Logs

@octet-stream octet-stream added the bug Something isn't working label Feb 17, 2025
@fgcoelho
Copy link

fgcoelho commented Mar 4, 2025

@octet-stream Have you tried including the plugin manually?

I don't know if it will work because this issue seems a bit different from what I fixed in my pull request, but you may give it a try:

import pluginTransformClassProperties from "@babel/plugin-transform-class-properties";

const jiti = createJiti(import.meta.url, {
   transformOptions: {
     ts: true,
     babel: {
       plugins: [pluginTransformClassProperties],
     },
   },
 });

Use this in your mikro-orm pull request to see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants