microsoft / microsoft/TypeScript

Accessing protected computed property does not produce a compiler error

Open
#60,974 7 comments 1 reaction 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

🔎 Search Terms

protected computed no error bug

🕗 Version & Regression Information

This is the behavior in every version I tried (v3.3.3333, v4.0.5, v5.7.3), and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs

⏯ Playground Link

https://tsplay.dev/wjdn7m

💻 Code
declare class Foo {
  protected readonly baz = 42
}

declare class Bar {
  readonly foo: Foo
}

declare const bar: Bar

// @ts-expect-error
console.log(bar.foo["baz"].toFixed(2))
🙁 Actual behavior
  • (without // @ts-expect-error): No errors
  • (with // @ts-expect-error): Compiler error Unused '@ts-expect-error' directive
🙂 Expected behavior
  • (without // @ts-expect-error): Compiler error Property […] is protected and only accessible within class 'Foo' and its subclasses
  • (with // @ts-expect-error): No errors
Additional information about the issue

No response

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 behavior from the TypeScript Playground link using the provided Foo, Bar, and bracket-access example. Start by tracing compiler handling of protected-property access through an indexed access, then add coverage for the expected diagnostic and verify that the @ts-expect-error directive is consumed.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.