microsoft / microsoft/TypeScript

Protected nested classes

Open
#10,017 5 comments 18 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

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.

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

Reproduce the three TypeScript examples from the issue, including the namespace workaround and the subclass extension case. Trace how the compiler resolves nested class types and protected static members, then add regression coverage showing that the valid examples compile without errors while protected access remains enforced.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.