Don't show object property name completions when the originating type is a class with private or protected members #44888
Labels
Experience Enhancement
Noncontroversial enhancements
Help Wanted
You can do this
Suggestion
An idea for TypeScript
Milestone
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:
The
serve
function should only take aDish
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
🙁 Actual behavior
Dish
propertiesname
andallergens
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.The text was updated successfully, but these errors were encountered: