Skip to content

Setters not accounted for by strictNullChecks & strictPropertyInitialization #24937

Closed
@tbillington

Description

@tbillington

TypeScript Version: 2.9.2 & 3.0.0-dev.20180609

Search Terms:
setter

Code

When enabling the strictNullChecks and strictPropertyInitialization tsconfig options tsc produces an error when it shouldn't. It looks like the rule implementation of the aforementioned strict flags might not account for setters? Easily verifiable in the typescript repl too, once enabling the flags.

class Foo {
  private _x: number;
  public set x(nx: number) {
    this._x = nx;
  }
  constructor() {
    this.x = 1;
  }
}

tsc: Property '_x' has no initializer and is not definitely assigned in the constructor.

Expected behavior:
No errors.

Actual behavior:
tsc emits Property '_x' has no initializer and is not definitely assigned in the constructor. error.

Playground Link:
http://www.typescriptlang.org/play/#src=class%20Foo%20%7B%0D%0A%20%20%20%20private%20_x%3A%20number%3B%0D%0A%20%20%20%20public%20set%20x(nx%3A%20number)%20%7B%0D%0A%20%20%20%20%20%20%20%20this._x%20%3D%20nx%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20constructor()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.x%20%3D%201%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions