Skip to content

Typescript parameter decorators use incorrect scope #2147

@kanoshin

Description

@kanoshin

Example TS code:

import { inject } from 'inject';
import { Abc } from './Abc';

class Root1 {
    constructor(@inject(Abc) private Abc: Abc) {}
}

class Root2 {
    constructor(@inject(Abc) private abc: Abc) {}
}

Example esbuild output for esbuild src/index.ts --outfile=dist/bundle.js command:

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result)
    __defProp(target, key, result);
  return result;
};
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
import { inject } from "inject";
import { Abc } from "./Abc";
let Root1 = class {
  constructor(Abc2) {
    this.Abc = Abc2;
  }
};
Root1 = __decorateClass([
  __decorateParam(0, inject(Abc2))
], Root1);
let Root2 = class {
  constructor(abc) {
    this.abc = abc;
  }
};
Root2 = __decorateClass([
  __decorateParam(0, inject(Abc))
], Root2);

When running the output code there will be the error: Abc2 is not defined. Notice that esbuild thinks that in Root1 case decorator parameter is equal to constructor parameter. Same thing works properly when decorator parameter named differently from constructor parameter.

The same code will transpile to JS properly when using TSC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions