question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: The internal modifier for classes

See original GitHub issue

The internal modifier

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 the internal modifier to class members.

Goals

This proposal aims to describe the static semantics of the internal modifier as it applies to members of a class (methods, accessors, and properties).

Non-goals

This proposal does not cover any other use of the internal modifier on other declarations.

Static Semantics

Visibility

Within a non-declaration file, a class member marked internal is treated as if it had public visibility for any property access:

source.ts:

class C {
    internal x: number;
}

let c = new C();
c.x = 1; // ok

When consuming a class from a declaration file, a class member marked internal is treated as if it had private visibility for any property access:

declaration.d.ts

class C {
    internal x: number;
}

source.ts

/// <reference path="declaration.d.ts" />
let c = new C();
c.x = 1; // error

Assignability

When checking assignability of types within a non-declaration file, a class member marked internal is treated as if it had public visibility:

source.ts:

class C {
    internal x: number;
}

interface X {
    x: number;
}

let x: X = new C(); // ok

If one of the types is imported or referenced from a declaration file, but the other is defined inside of a non-declaration file, a class member marked internal is treated as if it had private visibility:

declaration.d.ts

class C {
    internal x: number;
}

source.ts

/// <reference path="declaration.d.ts" />
interface X {
}

let x: X = new C(); // error

It is important to allow assignability between super- and subclasses from a declaration file with overridden members marked internal. When both types are imported or referenced from a declaration file, a class member marked internal is treated as if it had protected visibility:

declaration.d.ts

class C {
    internal x(): void;
}

class D extends C {
    internal x(): void;
}

source.ts

/// <reference path="declaration.d.ts" />
let c: C = new D(); // ok

However, this does not carry over to subclasses that are defined in a non-declaration file:

declaration.d.ts

class C {
    internal x(): void;
}

source.ts

/// <reference path="declaration.d.ts" />
class D extends C {
    internal x(): void; // error
}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:357
  • Comments:93 (13 by maintainers)

github_iconTop GitHub Comments

15reactions
EisenbergEffectcommented, Apr 22, 2018

@rbuckton Is there any status on this? It’s been 2.5 years since this was proposed. I’m working on Aurelia vNext right now, completely written in TS. We could really make use of this feature…

15reactions
matthewmuellercommented, Mar 10, 2018

I’d like to see this too, but have you guys considered the way Golang handles this? I find their approach quite elegant.

They let you have a directory called /internal, which only allows access to internal modules in the folder that contains that internal folder (and access from within that internal folder)

Graphically it looks like this:

/project
  /internal
    /component
      component.go // internal
  /api
    api.go // access to internal component
  main.go // access to internal component

/another-project
  main.go // no access to project's internal component
Read more comments on GitHub >

github_iconTop Results From Across the Web

C# 7 Feature Proposal: New Access Modifier - CodeProject
Internal access enables access from any code in the same assembly. Private access is limited to code in the same class. C# also...
Read more >
Why does visual studio set the access modifier of a new class ...
When i use the context menu in the solution explorer to add/create a new class inside of a folder, it set the access...
Read more >
Access Modifiers - C# Programming Guide - Microsoft Learn
internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared public , ...
Read more >
Protected and friend access modifier for class members - TC39
Furthermore, there are some ideas and a proposal (see ideas for use of class static blocks, proposal) to extend the normal use case...
Read more >
Internal or public? | Fabulous adventures in coding
Suppose we have a sealed internal class C with a member M intended to be ... Should the accessibility modifier at ??? be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found