You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The following Query class, which accepts a Table instance won't be able to access items since it has a private access modifier.
classQuery<Item>{this.items: Item[];publicconstructor(table: Table<Item>){this.items=table.items.slice();// error: items is private bruh}// other methods go herepublicresults() : Item[]{returnthis.items;}}
Current way to fix that is to switch it with a public access modifier like the following:
classTable<Item>{publicitems: 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.
interfaceUser{name: string;age: number;}constt=newTable<User>();t.insertItem('alice-id',{name: 'Alice',age: 25});// the right wayt.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?
classTable<Item>{privateitems: Item[]allowQuery;// the hideously added syntax// ...}classQuery<Item>{this.items: Item[];publicconstructor(table: Table<Item>){this.items=table.items.slice();// no error, Query is permitted to access that property;}}classNonPermittedClass{this.items: Item[];publicconstructor(table: Table<Item>){this.items=table.items.slice();// error, NonPermittedClass is not permitted to access that property.}}interfaceUser{name: string;age: number;}constt=newTable<User>();t.items.push({name: 'Alice',age: 25})// error, can't do that! items is private!
Maybe protected is even the right word
```tsclassTable<Item>{protecteditems: Item[]withQuery;// 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.)
The text was updated successfully, but these errors were encountered:
davalapar
changed the title
Expose private properties selectively to other classes
Allow exposing of private properties selectively to other classes
Feb 8, 2019
Search Terms
Suggestion
Could it be possible to selectively expose private properties / methods to other Classes?
Use Cases
private
topublic
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:
The following Query class, which accepts a Table instance won't be able to access
items
since it has aprivate
access modifier.Current way to fix that is to switch it with a
public
access modifier like the following: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.Will a syntax like the following kind of permit the
items
property only accessible within theTable
class and theQuery
class?Maybe protected is even the right word
Or is it stupid? 🤣
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: