microsoft / microsoft/TypeScript

Proposal: Friend Declarations for classes

Open
#7,692 9 comments 69 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:
   friendFriendDeclarationNames;

  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;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by comparing this alternative with proposal #5228 and validating the grammar and examples against TypeScript's language and type-checking design; done means an agreed implementation of friend declarations covering private and protected access in the shown cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.