Skip to content

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

Closed
@iamchathu

Description

@iamchathu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions