microsoft / microsoft/TypeScript
Proposal: Friend Declarations for classes
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
NOTE: This proposal is an alternative to the proposal in #5228.
Overview
Often there is a need to share information on types within a program or package that should not be
accessed from outside of the program or package. While the public accessibility modifier allows
types to share information, is insufficient for this case as consumers of the package have access
to the information. While the private accessibility modifier prevents consumers of the package
from accessing information from the type, it is insufficient for this case as types within the
package also cannot access the information. To satisfy this case, we propose the addition of a FriendDeclaration to classes:
class A {
friend createA;
private constructor() { }
}
function createA() {
return new A();
}
A FriendDeclaration is a new declaration supported in the body of a ClassDeclaration or ClassExpression that indicates that the class, function, or namespace to which the supplied name resolves can treat the private and protected members of the class as if they were public.
The name referenced in the FriendDeclaration is resolved relative to the ClassDeclaration. The name must be in scope.
Grammar
ClassElement: ( Modified )
FriendDeclaration
FriendDeclaration:
friend FriendDeclarationNames ;
FriendDeclarationNames:
EntityName
FriendDeclarationNames , EntityName
Examples
Instantiate a class with a private constructor:
class A {
friend createA;
private constructor() { }
}
function createA() {
return new A();
}
Extend a class with a private constructor:
class A {
friend B;
private constructor() { }
}
class B extends A {
}
Access private members of an imported class:
// a.ts
import { update } from "./b";
export class A {
friend update;
private x: number;
}
// b.ts
import { A } from "./a";
export function update(a: A) {
a.x = 1;
}
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
L’issue non indica alcun file o test. Inizia confrontando questa alternativa con la proposta #5228 e validando la grammatica e gli esempi rispetto alla progettazione del linguaggio e del controllo dei tipi di TypeScript; il lavoro è completato quando esiste un’implementazione concordata delle dichiarazioni friend che copra l’accesso a private e protected nei casi mostrati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 25/100