Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing protected computed property does not produce a compiler error #60974

Open
parzhitsky opened this issue Jan 15, 2025 · 5 comments
Open

Comments

@parzhitsky
Copy link

πŸ”Ž 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

@jcalz
Copy link
Contributor

jcalz commented Jan 15, 2025

This is working as intended, duplicate #19335

@parzhitsky
Copy link
Author

parzhitsky commented Jan 15, 2025

(I'd respond in the original issue, but it is limited to collaborators only)

I think, there needs to be a distinction between intended usage of bracket notation where dot notation is available, and unavoidable usage of bracket notation where dot notation is not available. I.e., my original use case is using a symbol as property key, – I can't not use bracket notation:

const key = Symbol('key')

declare class Foo {
  protected readonly [key]: number
}

declare class Bar {
  readonly foo: Foo
}

declare const bar: Bar

console.log(bar.foo[key].toFixed(2)) // no errors πŸ™

Try it.

AFAICT, TypeScript parser already has a mechanism to differentiate literals (such as foo["bar"]) from expressions (such as foo["b" + "a" + "r"]).

@parzhitsky
Copy link
Author

Should I open a feature request?

@MartinJohns
Copy link
Contributor

How could you even distinguish the two cases?

@jcalz
Copy link
Contributor

jcalz commented Jan 15, 2025

You could possibly file a feature request, but similar things like #50485 have been declined. Not sure if someone has brought up symbol keys before, but I imagine you'll get the same response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants