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.

Protected nested classes

See original GitHub issue

TypeScript Version: 1.8.9

Code

class Outer {
    protected static Inner = class {}
    private myInner: Outer.Inner;
}

Expected behavior: No errors.

Actual behavior: Compiler error: Cannot find namespace 'Outer'. Line 3, Column 24

Code

class Outer {
    protected static Inner = class {}
    private myInner: Outer.Inner;
}
namespace Outer {}

Expected behavior: No errors.

Actual behavior: Compiler error: Module 'Outer' has no exported member 'Inner'. Line 3, Column 32

Code

export class Outer {
    protected static Inner = class {};
}

export class Child extends Outer {
    static temp = class extends Outer.Inner {}
}

Expected behavior: No errors.

Actual behavior: Compiler error: Property 'Inner' is protected and only accessible within class 'Outer' and its subclasses. Line 6, Column 35


The problem here is that there is no way to define a protected nested class in a way that makes it possible to use in a type declaration. The third example shows why I want to use this: I want a nested class that can only be extended by classes that extend the parent class.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:18
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
michalburger1commented, Sep 21, 2018

The compilation error for the first example now (in Typescript 3) reads:

'Outer' only refers to a type, but is being used as a namespace here.

I’m not sure if there’s a better way to do this but I managed to get it to compile using this workaround:

class Outer {
    protected static Inner = class {}
    private myInner: InstanceType<typeof Outer.Inner>;
}
0reactions
whzx5bybcommented, Jul 6, 2021

It seems there is a different error in Example 3 in 3.3.3 and later.

export class Outer {
    protected static Inner = class {};
}

export class Child extends Outer {
    static temp = class extends Outer.Inner {}
    //            ~~~~~ '(Anonymous class)' is referenced directly or indirectly in its own base expression.ts(2506)
}

And it is possible to inherite from a public reference to the protected nested class.

export class Outer {
  protected static A1 = class {}

  static A2 = Outer.A1;

  static B1 = class extends Outer.A1 {
    // Error
  }

  static B2 = class extends Outer.A2 {
    // OK, as expected.
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

protected/public Inner Classes - java - Stack Overflow
You can just think protected inner class is protected member, so it only access for class, package, subclass but not for the world....
Read more >
Nested Classes - Learning the Java Language
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing...
Read more >
Nested classes - cppreference.com
Like any member of its enclosing class, the nested class has access to all names (private, protected, etc) to which the enclosing class...
Read more >
Nested Types - C# Programming Guide | Microsoft Learn
Nested types of a class can be public, protected, internal, protected internal, private or private protected. However, defining a protected , ...
Read more >
Nested Classes in Java - GeeksforGeeks
As a member of its enclosing class, a nested class can be declared private, public, protected, or package private(default). Nested classes are ...
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