#Typescript 5 Decorator support in Nodejs (20)

14 messages · Page 1 of 1 (latest)

topaz violet
#

I'm using TS 5.0.4 & Node 20.2

When running my program Node throws an invalid or unexpected token error when applying my decorator to my class method.

My basic decorator:

export function Auth(role: string) {
  return function (originalMethod: any, context: ClassMethodDecoratorContext) {
    const methodName = String(context.name);

    function replacementMethod(this: any, ...args: any[]) {
      console.log("Role", role);
      console.log(`LOG: Entering method '${methodName}'.`);
      console.log(args);
      const result = originalMethod.call(this, ...args);
      console.log(`LOG: Exiting method '${methodName}'.`);
      return result;
    }

    return replacementMethod;
  };
}

export class TestClass {
  @Auth("ADMIN")
  testMethod() {
    console.log("test");
  }
}

I assumed node had support for Decorators or typescript would polyfill it?

lavish blaze
#

Are you using the experimentalDecorator flag?

topaz violet
#

No

#

I was under the impression those were for the legacy decorator format

lavish blaze
#

Node has support for legacy decorators but they are not good to say the least

topaz violet
#

Yeah, Having issues with using the legacy decorators.

The thing is, it works in the playground, but not under node. Is there a working polyfil for modern decorators

dark shadowBOT
#
sachaw#5079

Preview:```ts
...
return function (
originalMethod: any,
context: ClassMethodDecoratorContext
) {
const methodName = String(context.name)

  function replacementMethod(
    this: any,

...```

onyx canyon
#

@topaz violet have you figured it out?

topaz violet
#

Nope, I'm going to wait for better adoption of the spec

onyx canyon
#

all right