Skip to content

Allow exposing of private properties selectively to other classes #29823

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

Closed
5 tasks done
davalapar opened this issue Feb 8, 2019 · 2 comments
Closed
5 tasks done

Allow exposing of private properties selectively to other classes #29823

davalapar opened this issue Feb 8, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@davalapar
Copy link

davalapar commented Feb 8, 2019

Search Terms

  • classes
  • private properties
  • private methods
  • expose selectively

Suggestion

Could it be possible to selectively expose private properties / methods to other Classes?

Use Cases

  • What do you want to use this for?
    • When we want a property/method of a class to be only accessible to other classes only.
  • What shortcomings exist with current approaches?
    • Currently we have to change the access modifier from private to public to permit other classes in accessing such property, but this isn't ideal since there are times this could be way too permissive.

Examples

Say we have a Table class:

class Table <Item> {
  private items: Item[];
  private ids: string[];
  public constructor () {
    this.ids = [];
    this.items = [];
  }
  public insertItem (id: string, data: Item) : void {
    this.ids.push(id);
    this.data.push(data);
  }
  public removeItemById (id: string) : void {
    const index = this.ids.indexOf(id);
    this.ids.splice(index , 1);
    this.items.splice(index , 1);
  }
}

The following Query class, which accepts a Table instance won't be able to access items since it has a private access modifier.

class Query <Item> {
  this.items: Item[];
  public constructor (table: Table<Item>) {
    this.items= table.items.slice(); // error: items is private bruh
  }
  // other methods go here
  public results () : Item[] {
    return this.items;
  }
}

Current way to fix that is to switch it with a public access modifier like the following:

class Table <Item> {
  public items: Item[];
  // ...
}

But that will have unwanted side effects such as other developers being mistakenly thinking that he/she may directly modify the items property since it is exposed publicly.

interface User {
  name: string;
  age: number;
}

const t = new Table <User> ();

t.insertItem('alice-id', { name: 'Alice', age: 25 }); // the right way

t.items.push({ name: 'Alice', age: 25 }) // woops, this shouldn't be possible!

Will a syntax like the following kind of permit the items property only accessible within the Table class and the Query class?

class Table <Item> {
  private items: Item[] allow Query; // the hideously added syntax
  // ...
}

class Query <Item> {
  this.items: Item[];
  public constructor (table: Table<Item>) {
    this.items= table.items.slice(); // no error, Query is permitted to access that property;
  }
}

class NonPermittedClass {
  this.items: Item[];
  public constructor (table: Table<Item>) {
    this.items= table.items.slice(); // error, NonPermittedClass is not permitted to access that property.
  }
}

interface User {
  name: string;
  age: number;
}
const t = new Table <User> ();
t.items.push({ name: 'Alice', age: 25 }) // error, can't do that! items is private!

Maybe protected is even the right word

```ts
class Table <Item> {
  protected items: Item[] with Query; // the hideously added syntax
  // ...
}

Or is it stupid? 🤣

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@davalapar davalapar changed the title Expose private properties selectively to other classes Allow exposing of private properties selectively to other classes Feb 8, 2019
@dragomirtitian
Copy link
Contributor

Very similar with #7692, #2136

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Feb 8, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants