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

Don't show object property name completions when the originating type is a class with private or protected members #44888

Closed
TheMrZZ opened this issue Jul 4, 2021 · 0 comments · Fixed by #45044
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@TheMrZZ
Copy link

TheMrZZ commented Jul 4, 2021

Bug Report

TypeScript currently autocompletes class properties in function arguments. This is an inherently wrong behavior, which leads to confusion and potential errors.

Let's take this code as an example:

class Dish {
  name: string
  protected allergens: string[]

  constructor (name: string, allergens: string[] = []) {
    this.name = name
    this.allergens = allergens
  }
}

function serve(dish: Dish) {
  console.log('Who wants some', dish.name, '?')
}

The serve function should only take a Dish as an argument. Nothing else should be allowed. Yet, TypeScript tries to autocomplete the class properties, if you type an object:

dish.mp4

Of course, you end up with an error, since the object cannot be interpreted as a Dish: Property 'allergens' is protected but type '{ name: string; allergens: string[]; }' is not a class derived from 'Dish'. (2345).
If you remove the allergens property, you'll have another error: Property 'allergens' is missing in type '{ name: string; }' but required in type 'Dish'. (2345).

This is very problematic, especially when your arguments don't just take a class instance, but also an object (like Dish | { somethingElse: string }). You then get an even more confusing autocompletion list.

🔎 Search Terms

TypeScript autocompletes class properties in function arguments.
TypeScript autocompletes class properties in function parameters.
TypeScript incorrectly autocompletes class instances properties, even with protected or private members.
TypeScript hints class properties in autocompletion when argument is either an object or a class instance.

🕗 Version & Regression Information

This is the behavior in every version I tried, including the latest 4.4 builds, and I reviewed the FAQ for all entries.

⏯ Playground Link

Playground link with relevant code

💻 Code

class Dish {
  name: string
  protected allergens: string[]

  constructor (name: string, allergens: string[] = []) {
    this.name = name
    this.allergens = allergens
  }
}

function serve(dish: Dish) {
  console.log('Who wants some', dish.name, '?')
}

// Property 'allergens' is protected but type '{ name: string; allergens: string[]; }' is not a class derived from 'Dish'. (2345)
serve({
  name: 'cake',
  allergens: ['chocolate'],
})

//   Property 'allergens' is missing in type '{ name: string; }' but required in type 'Dish'. (2345)
serve({
  name: 'cake',
})

// Correct sample
const cake = new Dish('cake', ['chocolate'])
serve(cake)

🙁 Actual behavior

Dish properties name and allergens are proposed to autocompletion. However, they shouldn't be, because even if you specify them all, the input will still be incorrect.

🙂 Expected behavior

No Dish property should be autocompleted. In my example, the autocompletion list should be empty when opening an object.

@RyanCavanaugh RyanCavanaugh changed the title Stop autocompleting class properties in function arguments Don't show object property name completions when the originating type is a class with private or protected members Jul 6, 2021
@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Jul 6, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jul 6, 2021
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Jul 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
2 participants