Skip to content

regression with private class members and loosing visibility #21516

Closed
@koblas

Description

@koblas

Upgrading from TypeScript 2.6.2 to 2.7.1 and got a private variable scoping problem.

TypeScript Version: 2.7.1

Search Terms:

private property does not exist

Code

class Parent {
  pubValue: number;

  private priValue: number;
  private priXXX: number;

  constructor(value: number) {
    this.priValue = value;
    this.pubValue = 7;
  }

  cloneImpl(kind: boolean): Child | OtherChild {
    const copy = kind ? new Child(this.priValue) : new OtherChild(this.priValue);
    copy.pubValue = this.pubValue + 1;
    copy.priXXX = this.priXXX;

    return copy;
  }
}

class Child extends Parent {
  myData: number;

  clone() {
    return this.cloneImpl(true) as Child;
  }
}

class OtherChild extends Parent {
  clone() {
    return this.cloneImpl(false) as OtherChild;
  }
}

const child = new Child(6);

child.clone();

Expected behavior:

In typescript 2.6.2 the code was working and not generating an error. Since cloneImpl() is in the parent class it should have access to it's own private (tried with protected as well) variables.

Actual behavior:

test.ts (16,10): Property 'priXXX' does not exist on type 'Child | OtherChild'. (2339)

Playground Link:

https://www.typescriptlang.org/play/#src=class%20Parent%20%7B%0D%0A%20%20pubValue%3A%20number%3B%0D%0A%0D%0A%20%20protected%20priValue%3A%20number%3B%0D%0A%20%20protected%20priXXX%3A%20number%3B%0D%0A%0D%0A%20%20constructor(value%3A%20number)%20%7B%0D%0A%20%20%20%20this.priValue%20%3D%20value%3B%0D%0A%20%20%20%20this.pubValue%20%3D%207%3B%0D%0A%20%20%7D%0D%0A%0D%0A%20%20cloneImpl(kind%3A%20boolean)%3A%20Child%20%7C%20OtherChild%20%7B%0D%0A%20%20%20%20const%20copy%20%3D%20kind%20%3F%20new%20Child(this.priValue)%20%3A%20new%20OtherChild(this.priValue)%3B%0D%0A%20%20%20%20copy.pubValue%20%3D%20this.pubValue%20%2B%201%3B%0D%0A%20%20%20%20copy.priXXX%20%3D%20this.priXXX%3B%0D%0A%0D%0A%20%20%20%20return%20copy%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Child%20extends%20Parent%20%7B%0D%0A%20%20myData%3A%20number%3B%0D%0A%0D%0A%20%20clone()%20%7B%0D%0A%20%20%20%20return%20this.cloneImpl(true)%20as%20Child%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20OtherChild%20extends%20Parent%20%7B%0D%0A%20%20clone()%20%7B%0D%0A%20%20%20%20return%20this.cloneImpl(false)%20as%20OtherChild%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20child%20%3D%20new%20Child(6)%3B%0D%0A%0D%0Achild.clone()%3B

Related Issues:

None found

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions