Skip to content

Incomplete Metadata for subclass with default constructor #13410

Description

TypeScript Version: 2.1.4

tsc is run with --experimentalDecorators and --emitDecoratorMetadata

Code

function foo(_: any) {}

@foo
class Foo {
    constructor(public bar: number) {}
}
@foo
class Bar extends Foo {
}

Expected behavior:

 Bar = __decorate([
     foo,
-    __metadata("design:paramtypes", [])
+    __metadata("design:paramtypes", [Number])
 ], Bar);

Metadata for subclass with default constructor should include parameter types of super constructor.

Actual behavior:

tsc emit:

/* ... helper functions ... */

function foo(_) { }
var Foo = (function () {
    function Foo(bar) {
        this.bar = bar;
    }
    return Foo;
}());
Foo = __decorate([
    foo,
    __metadata("design:paramtypes", [Number])
], Foo);
var Bar = (function (_super) {
    __extends(Bar, _super);
    function Bar() {
        return _super.apply(this, arguments) || this;
    }
    return Bar;
}(Foo));
Bar = __decorate([
    foo,
    __metadata("design:paramtypes", [])
], Bar);

Activity

  1. mhegazy commented on Jan 11, 2017

    @mhegazy
    Contributor

    This is behaving as intended. the derived class does not have a constructor, the compiler can not just delegate the constructor in the general case. Consider a case where the type of the argument in the base class is not accessible to the derived. the emit would be invalid. Noting that the output generation needs to work as single file transformation (i.e. in isolation of the rest of the project) . e.g.:

    // File1.ts
    class Inner { }
    export class Base {
        constructor (p: Inner) {}
    }
    //File2.ts
    import {Base} from "./File1";
    class Derived extends Base {} // Inner here is not visible

    If you are using TS 2.2 (specifically the change to __extends in #12488) you should be able to get the parent meta data by walking up the prototype chain.

  2. ajafff commented on Jan 11, 2017

    @ajafff
    ContributorAuthor

    That sounds pretty reasonable.
    Feel free to close this issue.

  3. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions