How to set "paramtypes" metadata in Javascript?

I'm currently working on NodeJs/Javascript setup (no typescript) that relies on decorators. Unfortunately I'm not able to convince swc to emit correct metadata. Here's an example
function decorator() {}

class Foo {
/**
* @param {string} x
*/
@decorator
foo(x) {
return 'foo';
}
}
function decorator() {}

class Foo {
/**
* @param {string} x
*/
@decorator
foo(x) {
return 'foo';
}
}
Compiling it with the following .swcrc
{
"$schema": "https://swc.rs/schema.json",
"module": {
"type": "commonjs"
},
"jsc": {
"target": "es2022",
"parser": {
"syntax": "ecmascript",
"decorators": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"externalHelpers": true,
"loose": true,
"keepClassNames": true,
},
"sourceMaps": false
}
{
"$schema": "https://swc.rs/schema.json",
"module": {
"type": "commonjs"
},
"jsc": {
"target": "es2022",
"parser": {
"syntax": "ecmascript",
"decorators": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"externalHelpers": true,
"loose": true,
"keepClassNames": true,
},
"sourceMaps": false
}
gives me
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _ts_decorate = require("@swc/helpers/_/_ts_decorate");
const _ts_metadata = require("@swc/helpers/_/_ts_metadata");
function decorator() {}
let Foo = class Foo {
foo(x) {
return 'foo';
}
};
_ts_decorate._([
decorator,
_ts_metadata._("design:type", Function),
_ts_metadata._("design:paramtypes", [
void 0 // <<---- How would I get `String` here?
]),
_ts_metadata._("design:returntype", void 0)
], Foo.prototype, "foo", null);
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _ts_decorate = require("@swc/helpers/_/_ts_decorate");
const _ts_metadata = require("@swc/helpers/_/_ts_metadata");
function decorator() {}
let Foo = class Foo {
foo(x) {
return 'foo';
}
};
_ts_decorate._([
decorator,
_ts_metadata._("design:type", Function),
_ts_metadata._("design:paramtypes", [
void 0 // <<---- How would I get `String` here?
]),
_ts_metadata._("design:returntype", void 0)
], Foo.prototype, "foo", null);
Does anybody know how to get swc to emit correct design:paramtypes metadata in a JS setup?
Solution:
You can’t
Jump to solution
7 Replies
Solution
kdy1
kdy13w ago
You can’t
kdy1
kdy13w ago
Decorator metadata is for typescript
simne7
simne7OP3w ago
I can't do it now 😉 How about leveraging JsDoc here to enable metadata output from Js source?
kdy1
kdy13w ago
I’m against it unless official tsc does it
simne7
simne7OP3w ago
Fair enough. Would you be open to accept PRs for a feature like this?
kdy1
kdy13w ago
Yeap, but only if tsc do the same
simne7
simne7OP3w ago
Well, ok. I guess they won't... nevermind, thanks for your quick reply!

Did you find this page helpful?