Skip to content

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

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

Open
Jamesernator opened this issue Oct 23, 2023 · 4 comments Β· May be fixed by #56183
Open

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

Jamesernator opened this issue Oct 23, 2023 · 4 comments Β· May be fixed by #56183
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@Jamesernator
Copy link

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

@fatcerberus
Copy link

fatcerberus commented 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 is ExampleInit.

@RyanCavanaugh
Copy link
Member

A private property can be optional, so it's possible that it's possible to instantiate with an object literal

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Experience Enhancement Noncontroversial enhancements labels Oct 23, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 23, 2023
@fatcerberus
Copy link

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.

@trusktr
Copy link
Contributor

trusktr commented Dec 17, 2023

Just fyi the autocomplete suggestion produces code that becomes a SyntaxError at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
4 participants