microsoft / microsoft/TypeScript

Don't suggest private fields in contexts where they are out of scope

Open
#56,177 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

private fields suggestions

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about private fields
⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAogHgQwLZgDYQJIDsCWwoC8UA3gFBQVQBmA9jQFxRYCuSARhAE4A0pAvgG5SpUJFiIU6ADI4A1tCLxkaaAB9xy9NjxDSAY1QIAzkY2ToZSlCPAEwHHqg4jS8wAoAbglTMIjBFggAJSMXj7QzmYqJORWlJwQwMycWFAAxLQ0TqlhvkJWfMJWNnYO1Jw0SG4QEioy8oyudXIQIVHoMXGUOFRQbk3oAHTOAxDVtdItQUGdXXEJSSlQNZoQ9RD5c4VzUAvJqVgQAO7tYyvm60GbFIWxFAkIACY0WKgg6ZmMLOxculZ6LxsnGYemANE4bmI1DoUD4jQmmFwwBmljmwAAFs5BhkYURMtdYfxhACsDZlgjCKdBlQKlVURQAPQMqAAFU471EOCwAHMoAAiKh8qABR7WCDQDF2KAYrgRUwIazMbnciA2HAvaGcD50fhXUhAA

💻 Code
type ExampleInit = {
    foo: number,
};

type ExampleLike = Example | ExampleInit;

class Example {
    static isExample(value: any): value is Example {
        return #foo in value;
    }

    static from(exampleLike: ExampleLike): Example {
        if (Example.isExample(exampleLike)) {
            return exampleLike;
        }
        return new Example(exampleLike);
    }

    readonly #foo: number;

    constructor({ foo }: ExampleInit) {
        this.#foo = foo;
    }
}

const example = Example.from({
    // Try typing "f" and see that there is a suggestion for #foo
});
🙁 Actual behavior

We get suggestions for private fields:

Screenshot from 2023-10-23 15-45-37

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.

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

Start by reproducing the completion in the linked TypeScript Playground using the provided Example class and object literal. Trace the completion behavior for the private field outside the class scope, then verify that the suggestion is omitted there without changing valid private-field suggestions inside the defining class.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.