Describe the bug
When using a parameter decorator with a parameter of type interface, swc does not remove that interface, leading to errors at runtime.
Input code
// sub.ts
export interface Interface {
p: string
}
// main.ts
import { Interface } from './sub.js'
function ParameterDecorator() {
return function(
target: any,
propertyKey: string | symbol,
parameterIndex: number,
) { }
}
class Class {
public constructor(
@ParameterDecorator()
i: Interface,
) { }
}
const c = new Class({ p: '' })
console.log('success')
Config
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true
},
"target": "es2022",
"loose": false,
"externalHelpers": false,
"keepClassNames": true
},
"sourceMaps": true,
"module": {
"type": "es6"
},
"minify": false
}
Playground link
https://replit.com/@8NAF/swc-issues-7101
Expected behavior
The interface should be removed.
Actual behavior
No response
Version
1.3.41
Additional context
I can fix this issue by using import type { Interface } from './sub.js', but I still opened the issue for the following 3 reasons:
- I am using
typescript-eslint, and it will automatically remove type when importing with interfaces (specifically, with this rule).
- The interface will be removed if it is not used with a parameter decorator, but it will not be removed if it is used with one, which causes inconsistency.
tsc can handle this case normally without any runtime errors.
Describe the bug
When using a parameter decorator with a parameter of type interface,
swcdoes not remove that interface, leading to errors at runtime.Input code
Config
{ "$schema": "https://json.schemastore.org/swcrc", "jsc": { "parser": { "syntax": "typescript", "decorators": true, "dynamicImport": true }, "transform": { "decoratorMetadata": true, "legacyDecorator": true }, "target": "es2022", "loose": false, "externalHelpers": false, "keepClassNames": true }, "sourceMaps": true, "module": { "type": "es6" }, "minify": false }Playground link
https://replit.com/@8NAF/swc-issues-7101
Expected behavior
The interface should be removed.
Actual behavior
No response
Version
1.3.41
Additional context
I can fix this issue by using
import type { Interface } from './sub.js', but I still opened the issue for the following 3 reasons:typescript-eslint, and it will automatically removetypewhen importing with interfaces (specifically, with this rule).tsccan handle this case normally without any runtime errors.