You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Don't suggest private fields in contexts where they are out of scopeΒ #56177
typeExampleInit={foo: number,};typeExampleLike=Example|ExampleInit;classExample{staticisExample(value: any): value is Example{return #foo invalue;}staticfrom(exampleLike: ExampleLike): Example{if(Example.isExample(exampleLike)){returnexampleLike;}returnnewExample(exampleLike);}readonly #foo: number;constructor({ foo }: ExampleInit){this.#foo =foo;}}constexample=Example.from({// Try typing "f" and see that there is a suggestion for #foo});
π Actual behavior
We get suggestions for private fields:
This is particularly annoying as the private fields tend to be suggested first, so tab-completion tends to produce broken code.
π Expected behavior
The private field should not be included as a suggestion when outside the class scope where the field is defined.
Since the proposal is to make this context-sensitive anyway: Technically you shouldn't get suggested anything from Example (not even public properties) in this context because the existence of a private property means the type can't possibly be satisfied by a bare object literal. The only type that object literal can satisfy is ExampleInit.
Does it ever make sense for a #private field to be optional? You can instantiate such a type with an object literal in theory, but any method that tries to access the property on that object will error at runtime--optional or not.
Activity
fatcerberus commentedon Oct 23, 2023
Since the proposal is to make this context-sensitive anyway: Technically you shouldn't get suggested anything from
Example(not even public properties) in this context because the existence of a private property means the type can't possibly be satisfied by a bare object literal. The only type that object literal can satisfy isExampleInit.RyanCavanaugh commentedon Oct 23, 2023
A private property can be optional, so it's possible that it's possible to instantiate with an object literal
fatcerberus commentedon Oct 23, 2023
Does it ever make sense for a
#privatefield to be optional? You can instantiate such a type with an object literal in theory, but any method that tries to access the property on that object will error at runtime--optional or not.trusktr commentedon Dec 17, 2023
Just fyi the autocomplete suggestion produces code that becomes a
SyntaxErrorat runtime.