Preserve comments with object destructuring assignment
See original GitHub issueSearch Terms
object destructuring assignment, comment
Suggestion
interface MyInterface {
/**
* I am x
*/
x : number;
/**
* I am y
*/
y : number;
}
declare const myInterface : MyInterface;
const {x, y} = myInterface;
/**
* Expected tooltip to have comment,
* > I am x
*
* Actual: No comment
*/
x;
/**
* Expected tooltip to have comment,
* > I am y
*
* Actual: No comment
*/
y;
/**
* Expected tooltip to have comment,
* > I am x
*
* Actual:
* Tooltip has comment,
* > I am x
*/
myInterface.x;
/**
* Expected tooltip to have comment,
* > I am y
*
* Actual:
* Tooltip has comment,
* > I am y
*/
myInterface.y;
/**
* I am z
*/
const z = 1;
/**
* Expected tooltip to have comment,
* > I am z
*
* Actual:
* Tooltip has comment,
* > I am z
*/
z;
Use Cases
I came across this idea after writing code like this,
function foo (
{
somePropertyA,
somePropertyB,
somePropertyC,
} : SomeObject
) {
/* Use these properties */
}
And I was hazy on the details of what each property was for.
So, I hovered my cursor over the variables somePropertyA, somePropertyB, somePropertyC
and noticed there were no comments.
I had to go to the declaration of somePropertyA, somePropertyB, somePropertyC
and look at each property individually.
At the moment, the way to get comments is to just do,
function foo (
o : SomeObject
) {
/* Use these properties */
}
Then the tooltip for o.somePropertyA, o.somePropertyB, o.somePropertyC
will have comments
Examples
See above suggestion
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Destructuring assignment in function call while preserving ...
I would like to deconstruct the object inside the function parameters while also passing the object to the function so that the object...
Read more >JavaScript object destructuring usages you must know
Introduction. We use JavaScript objects to store data and retrieve it later. We store data(aka information) in key-value pairs.
Read more >Destructuring assignment
Destructuring assignment is a special syntax that allows us to “unpack” arrays or objects into a bunch of variables, as sometimes that's ...
Read more >How to Use Object Destructuring in JavaScript
The object destructuring is a useful JavaScript feature to extract properties from objects and bind them to variables.
Read more >How To Use Destructuring Assignment In JavaScript
JavaScript provides you with the ability to destructure objects and assign the individual units in one take. Destructuring means to unpack ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Let’s take react hooks as an example.
I have a hook called
useForm
that returns an object. The react way would be to do this:In this situation any JSDoc comments for
bind
oronSubmit
are not passed on from the output ofuseForm
to the destructured variables. I would argue that even if I use{onSubmit: formSubmit}
the documentation foronSubmit
still applies.If the typings are passed over why is the documentation not?
I have an example on the playground that shows what I mean.
I get that these are the same:
but I would have said in both instances I would expect the documentation to copy over.
x
is stillsomeObject.x
and unless I redfine it in some way its still the same.There should be a way to document/annotate the destructured variables themselves. It should work with the JsDoc comment syntax shown in this Stack Overflow answer:
When hovering over the
foo
variable in the last line, I would expect to see the “variable doc” in the IntelliSense variable description.