Skip to content

Issue when class property initialization happen inside, a method called inside of constructor #33347

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
iamchathu opened this issue Sep 10, 2019 · 1 comment

Comments

@iamchathu
Copy link

iamchathu commented Sep 10, 2019

TypeScript Version: Version 3.5.3

Search Terms:

Class property initialization, error TS2564: Property has no initializer and is not definitely assigned in the constructor.

Code

export class NetworkConnection extends EventEmitter {

  private intervalId: NodeJS.Timeout;

  public constructor(timeout = 60000) {
    super();
    this.start(timeout);
  }

  public start = (timeout: number) => {
    this.intervalId = setInterval(this.broadcast, timeout);  
  };

  public stop = () => {
    clearInterval(this.intervalId);
  };

...
}

Expected behavior:

Run without compile error.

Actual behavior:

error TS2564: Property 'intervalId' has no initializer and is not definitely assigned in the constructor.

Playground Link:

Related Issues: #24446 #21132

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 10, 2019

Duplicate of #21132, not just a related issue. And #30462 is an open proposal regarding this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants