GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — regression with private class members and loosing visibility · Issue #21516 · microsoft/TypeScript

regression with private class members and loosing visibility #21516

New issue
Copy link
New issue
Copy link
Closed
Closed
regression with private class members and loosing visibility#21516
Copy link
Labels
Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing code

Description

@koblas
David Koblas (koblas)
opened on Jan 31, 2018
Issue body actions

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

Activity

  1. David Koblas (koblas)
    changed the title [-]regression with private class members and loosing visibility.[/-] [+]regression with private class members and loosing visibility[/+] on Jan 31, 2018
  2. mhegazy commented on Jan 31, 2018

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Jan 31, 2018
    Contributor
    More actions

    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);
  3. Mohamed Hegazy (mhegazy)
    added
    Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing code
    on Jan 31, 2018
  4. Mohamed Hegazy (mhegazy)
    closed this as completedon Mar 9, 2018
  5. microsoft
    locked and limited conversation to collaborators on Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeWould 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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.