Skip to content

Unable to use 2022-03 decorators with swc-node/register #773

Open
@alberteddu

Description

@alberteddu

Hey! I am trying to use the new decorators in this way:

.swcrc

{
    "$schema": "https://json.schemastore.org/swcrc",
    "jsc": {
        "parser": {
            "syntax": "typescript",
            "decorators": true
        },
        "transform": {
            "decoratorVersion": "2022-03",
        }
    }
}

test.ts

function one(_target: unknown, context: ClassFieldDecoratorContext) {
    context.addInitializer(function () {
        const instance = this as { prop: number };
        instance.prop = 1;
    });
}

class Example {
    @one
    prop: number;
}

const instance = new Example();
console.log(instance.prop); // Output: undefined instead of 1

Using this command: SWCRC=true node --require @swc-node/register test.ts

The output is expected to be 1 (that is the output I get with ts-node) but is undefined with @swc-node/register.
I have looked around and I am not sure if this is supposed to work yet.
Am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions