question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preserve comments with object destructuring assignment

See original GitHub issue

Search 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;

Playground

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:open
  • Created 4 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
Arcathcommented, Oct 11, 2019

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:

const MyComponent: React.FC = () => {
  const {bind, onSubmit} = useForm({name: ''})

  // Rest of component
}

In this situation any JSDoc comments for bind or onSubmit are not passed on from the output of useForm to the destructured variables. I would argue that even if I use {onSubmit: formSubmit} the documentation for onSubmit 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:

const x = someObject.x
const {x} = someObject

but I would have said in both instances I would expect the documentation to copy over. x is still someObject.x and unless I redfine it in some way its still the same.

0reactions
ab-pmcommented, Aug 10, 2022

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:

interface Example {
    /** interface property doc */
    foo: string;
}
const example: Example = {foo: 'bar'};

const {
    /** variable doc */
    foo
} = example;

console.log(foo);

When hovering over the foo variable in the last line, I would expect to see the “variable doc” in the IntelliSense variable description.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found