Skip to content

Unexpected "A 'super' call must be the first statement" #1682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eschwartz opened this issue Jan 15, 2015 · 1 comment
Closed

Unexpected "A 'super' call must be the first statement" #1682

eschwartz opened this issue Jan 15, 2015 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@eschwartz
Copy link

From #945

The first statement in the body of a constructor must be a super call if both of the following are true:
• The containing class is a derived class.
• The constructor declares parameter properties or the containing class declares instance member variables with initializers.

These don't seem to be the only two conditions.

Consider this:

class MyView extends Backbone.View {
   private someProp:SomeType;

  constructor(options:MyOptionsType) {
     // I want to create my view with these configured events:
     options.events = {
       'click .btn-close': this.close
     };
     super(options);

     this.someProp = new SomeType();
  }
}

This results in a compiler error: error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties.

The MyView class does not contain initialized properties or parameter properties. What's going on here?

@eschwartz
Copy link
Author

wait, nm. This was caused by my own mistake. sorry...

For anyone else having this issue, here's how I got confused

class MyView extends Backbone.View {
   private someProp:SomeType;

  constructor(options:MyOptionsType) {
     // I want to create my view with these configured events:
     options.events = {
       'click .btn-close': this.close
     };
     super(options);

     this.someProp = new SomeType();
  }

  // Converted incorrectly from JS 
  // MyView.prototype.mySuperCoolMethod = function() { }
  // Should be:
  // private mySuperCoolMethod() {}
  // Otherwise TypeScript considers this an "initialized property".
  private mySuperCoolMethod = function() {
  }
}

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jan 15, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants