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

Declare all missing members quick fix #28491

Closed
DanielRosenwasser opened this issue Nov 12, 2018 · 10 comments · Fixed by #44576
Closed

Declare all missing members quick fix #28491

DanielRosenwasser opened this issue Nov 12, 2018 · 10 comments · Fixed by #44576
Labels
Domain: Quick Fixes Editor-provided fixes, often called code actions. In Discussion Not yet reached consensus Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

In #28298 we added a new message that elaborated on several of the missing properties in a type. We should specialize the fix here.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. labels Nov 12, 2018
@weswigham weswigham added the In Discussion Not yet reached consensus label Nov 29, 2018
@Anima-t3d
Copy link

Any update on this, this would be very helpful.

@cagcak
Copy link

cagcak commented Oct 6, 2020

This possible feature might be very helpful. An equivalent action on intellij webstorm is the following:
equelivent-action-on-webstorm

@jaikjoy
Copy link

jaikjoy commented Oct 22, 2020

Do we have any updates on this?

@tamj0rd2
Copy link

tamj0rd2 commented Dec 26, 2020

I've made a typescript language server plugin and vscode extension that adds some of this functionality.

If this github issue gets approved with Help Wanted I'd be happy to whip up a PR

Update: I've made this extension a bit more comprehensive now and it should be working for a lot more use cases. Features:

  • declare missing object members for a variable (and anywhere nested inside)
  • declare missing object members for a function or constructor call (and anywhere nested inside)

Some basic examples:

Declaring missing members

@a-tarasyuk
Copy link
Contributor

@DanielRosenwasser It seems this issue has many reactions :). I think fixAddMissingMember resolves this issue partially. Could you add more cases that need to add to fixAddMissingMember?

@DanielRosenwasser DanielRosenwasser added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Jan 19, 2021
@DanielRosenwasser
Copy link
Member Author

Seems like you'd need to reconstruct the information about which object literal is causing the missing properties issue. If you can get that object literal, you can totally imagine something where:

  1. For any missing property whose name exists locally (but maybe not in the global scope), use that as a shorthand property. Potentially, check that the types are compatible if you can first.
  2. If that fails, see if the property type has a call signature. If so, scaffold out a method body like throw new Error("not implemented!")
  3. Otherwise, create a property declaration and set it to undefined. The user will have to correct it manually.

@chris-schmitz
Copy link

I would definitely love this feature too, just a quick fix to fill everything in would definitely be nice.

That said, for anyone looking for this feature there is a way of doing it now though it's not as easy as a quick fix to fill everything in.

VSCode has a command called "Trigger Suggest" that's used to show typeahead suggestions based on the current code context. In the case of missing type definitions of an interface, because the compiler knows the type definition and what's currently missing you can use the suggestion trigger to get the list of suggested properties.

You can trigger this either from the command pallet or from the keyboard shortcut shift + cmd + , (I'm pretty sure that's the default and not a remapping I did and forgot about :P )

trigger suggest

Definitely not as nice as a quick fix that shortcuts all of the properties for you, but still helpful.

@tamj0rd2
Copy link

tamj0rd2 commented Feb 14, 2021

Seems like you'd need to reconstruct the information about which object literal is causing the missing properties issue. If you can get that object literal, you can totally imagine something where:

  1. For any missing property whose name exists locally (but maybe not in the global scope), use that as a shorthand property. Potentially, check that the types are compatible if you can first.
  2. If that fails, see if the property type has a call signature. If so, scaffold out a method body like throw new Error("not implemented!")
  3. Otherwise, create a property declaration and set it to undefined. The user will have to correct it manually.

@DanielRosenwasser Could something like the extension I posted above be useful? It doesn't check for variables in scope to use as a shorthand, but I could spend a bit of time looking into that. For your 3rd point, it instead gives you a default value. e.g if the property is of type number, it will make a property declaration of 0. If it's a string, it will do "todo".

I'm keen on starting a PR if this is something the TS team would be happy with!

Edit: This is all implemented in the extension I posted above. Still very keen to do a PR if the team's happy with the approach.

@DanielRosenwasser
Copy link
Member Author

Thanks @a-tarasyuk!

@connorjclark
Copy link
Contributor

connorjclark commented Jun 17, 2021

For extremely nested/deep object types, this QF would do too much, especially if you are passing references to other objects around.

Example: you have a car in local scope, and are missing property car on a Purchase object: preference would be to autofill car: undefined or car: {} on the Purchase object literal, not {car: {...maybe hundreds of nested properties...}}.

Of course, this is context-specific, so I suggest a second QF: "Declare all missing members (shallow)".

There is a VSCode extension that does the same, here was my feedback: tamj0rd2/ts-quickfixes#183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Quick Fixes Editor-provided fixes, often called code actions. In Discussion Not yet reached consensus Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants