Skip to content

Incorrect emit of __metadata('design:paramtypes', []) when using emitDecoratorMetadata and inheritance #12439

@tbosch

Description

@tbosch

TypeScript Version: 2.1.1

Flags: emitDecoratorMetadata: true

Code

class SomeArg {}

function SomeDecorator(): any {}

@SomeDecorator()
class Parent {
  constructor(a: SomeArg) {}
}

@SomeDecorator()
class Child extends Parent {
}

Expected behavior:
Does not emit __metadata('design:paramtypes', []) for Child or declares the parameters from the parent class, as Child did not declare an own constructor so the constructor of the parent class gets inherited.

I.e.

Parent = __decorate([
    SomeDecorator(),
    __metadata("design:paramtypes", [SomeArg])
], Parent);
...
Child = __decorate([
    SomeDecorator(),
], Child);

Actual behavior:
Emits __metadata('design:paramtypes', []) as part of the decorators for Child. This is wrong as the the constructor is inherited so Child actually takes one argument of type SomeArg, in contrast to the value of __metadata('design:paramtypes', []).

I.e.

Parent = __decorate([
    SomeDecorator(),
    __metadata("design:paramtypes", [SomeArg])
], Parent);
...
Child = __decorate([
    SomeDecorator(),
    __metadata("design:paramtypes", [])
], Child);

** Other notes
It would be really nice to be able to turn on flags as well as choose the typescript version in the typescript playground, so I could post a working reproduction here.

** Workaround
To detect that a class did not declare its own constructor we can use a regex on Child.toString():

const DELEGATE_CTOR = /^function\s+\w+\(\)\s*{\s*(return\s+)?\w+\.apply\(this,\s*arguments\)/;

function isDelegateCtor(ctor: any) {
  return DELEGATE_CTOR.exec(ctor.toString());
}

Note that this workaround also works with minified code as none of function / apply / arguments can be minified.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions