How to output decorators in compiled TypeScript

184 Views Asked by At

This is an example of a class inside an index.ts file that makes use of a decorator i.e myDecorator.

export class TestClass {
    @myDecorator()
    testFn() {}
}

When I compile this using tsc, it generates an index.d.ts and the output is as follows

export declare class TestClass {
    testFn(): void;
}

Is it possible to carry the decorator into the output e.g.

export declare class TestClass {
    @myDecorator()
    testFn(): void;
}

Here is my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./lib",
    "strict": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules",
    "**/__tests__/*"
  ]
}

I have tried a number of combinations of trying to carry the decorator to no avail.

1

There are 1 best solutions below

0
On

By definition, tc39 decorator cannot change the type in any way, so it's pointless in d.ts.

Use JSDoc if you want to mark its usage

export class TestClass {
    @myDecorator()
    /** @myDecorated */
    testFn() {}
}
// v //
export class TestClass {
    /** @myDecorated */
    testFn() {}
}

Probably that's possible to automate through a typescript code transformer, but I have no experience in them.