Skip to content
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

regression with private class members and loosing visibility #21516

Closed
koblas opened this issue Jan 31, 2018 · 1 comment
Closed

regression with private class members and loosing visibility #21516

koblas opened this issue Jan 31, 2018 · 1 comment
Labels
Breaking Change Would introduce errors in existing code

Comments

@koblas
Copy link

koblas commented Jan 31, 2018

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

@koblas koblas changed the title regression with private class members and loosing visibility. regression with private class members and loosing visibility Jan 31, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jan 31, 2018

This is a side effect of #19671. the type copy is now inferred as Child | OtherChild instead of just Child. private properties are not exposed on the union, since it is not always possible to know if they are applicable to the specific use case or not.
A work around would be to use a type assertion as:

 const copy = kind ? new Child(this.priValue) : <Child> new OtherChild(this.priValue);

@mhegazy mhegazy added the Breaking Change Would introduce errors in existing code label Jan 31, 2018
@mhegazy mhegazy closed this as completed Mar 9, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code
Projects
None yet
Development

No branches or pull requests

2 participants