'How to tell JS intellisense code completion that a function returns an object in the same format that it is passed?
I notice that when I use the cloneDeep function supplied by lodash, my code-completion figures out that the object being returned uses the same structure as the object being passed in.
However, if I write some custom function that does the same thing, the code-completion can not work out that the returned object has the same structure as the one passed in.
I'm not surprised about that, I'm surprised that it DOES work with lodash.cloneDeep! How do they do that? I've looked at the source code and can't see anything obvious. I even stole the JSDoc comment from above cloneDeep and put it above my function, but it doesn't work.
Any ideas?
Solution 1:[1]
I believe you need to require the class type you are returning in the file and use a JSDoc style comment above your custom function which defines the return value as the type you are returning.
const ExampleClass = require('exampleclass')
/**
* My Function
* @returns {ExampleClass}
*/
myFunction () {...}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Troy H |
