10000 Cannot do anything before super() · Issue #3311 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
Cannot do anything before super() #3311
@NekR

Description

@NekR

Consider this code:

class Super {
  _view:Element;

  constructor(view?:Element) {
    this._view = view || document.createElement('div');
  };
}

class Inherits extends Super {
  test = 1;
  view:Element;

  constructor() {
    let view = document.createElement('span');
    super(view);

    this.view = view;
  };
}

And it says: 2376 A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.

I understand that it's because of test property. but can you allow not-this code before super() call how ES6 does?

Also, this won't work too, of course:

class Inherits extends Super {
  test = 1;
  view:Element = document.createElement('span');

  constructor() {
    super(this.view); // 2332 'this' cannot be referenced in current location.
  };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".FixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0