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.

Discussion: How to document objects/interfaces in function arguments

See original GitHub issue

I’m currently trying to add TSDoc comments to a custom React hook that I’d like to release on npm. The only argument passed to the function is an optional config object which I’m destructuring. I can add a summary to the function fine, which works with IntelliSense, but when trying to document the properties of the config object I’m running into issues. I can’t see the description when I mouseover them.

I can’t see anything in the docs or online about doing this apart from this stackoverflow answer for JSdoc, which doesn’t seem to work.

Below is my function signature. Paste it into the TSDoc playground to see what I mean. I’ve tried abstracting the object types to a separate interface with their own TSDoc comments, as well as using inline TSDoc comments above each line, and they still don’t work.

I’d like users of my package to be able to see the descriptions of the config’s properties as they type or mouseover them. What is the proper way to do this? This is a problem for any functions that have object parameters including React components

/**
 * A custom hook to use `requestAnimationFrame` inside a React component
 *
 * @param config - An optional configuration object for the hook
 * @param config.onStart - A callback that will be run once when the animation starts
 * @param config.onFrame - A callback that will be run on every frame of the animation
 * @param config.onEnd - A callback that will be run on once when the animation ends
 * @param config.delay - A delay in ms after which the animation will start
 * @param config.endAfter - A time in ms after which the animation will be stopped
 * @param config.fps - The desired fps that the animation will be throttled to
 *
 * @returns An object containing the current elapsedTime, frameCount and fps of the animation, as well as a function to stop the animation
 */
const useAnimationFrame = ({
    onStart,
    onFrame,
    onEnd,
    delay,
    endAfter,
    fps: throttledFps
}: {
    onStart?: () => void;
    onFrame?: () => void;
    onEnd?: () => void;
    delay?: number;
    endAfter?: number;
    fps?: number;
} = {}) => {
    let elapsedTime, frameCount, fps, endAnimation;

    // Do stuff

    return { elapsedTime, frameCount, fps, endAnimation };
};

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

1reaction
neefrehmancommented, Jul 26, 2020

@octogonz Thanks for the insight! You’re right, without destructuring the docs are picked up correctly by VSCode.

width and height are part of the Canvas2DDrawProps interface, which contains a bunch of other QoL variables and callbacks that can be passed down for drawing to the canvas. My current version to test on my own projects is here. Its the first library I’m hoping to release, so your notes on simple patterns are useful.

1reaction
octogonzcommented, Jul 25, 2020

@neefrehman Without seeing a full code sample, I’m guessing that { width, height } in this expression is not actually typed as Canvas2DDrawProps. You seem to be relying heavily on type inference, so there may be some type algebra reason why the analyzer cannot safely apply the docs to width. (Or maybe it just isn’t smart enough and will get improved in some future compiler release?)

I can repro something similar with this code sample:

interface IOptions {
    /** The width */
    width: number;
    /** The height */
    height: number;
}

type DistanceFunction = (options: IOptions) => number;

const getDistance: DistanceFunction = ({width, height}) => {
    return width + height;
};

I was able to fix it by eliminating the destructuring parameter:

const getDistance: DistanceFunction = (options) => {
    return options.width + options.height;
};

If you are targeting ES5 for web browsers, the transpiled output will look like this. It feels old fashioned to say this, but life often is easier if we avoid fancy syntaxes and write code using simple, explicit patterns. This philosophy applies especially to public APIs that need to be documented and consumed by an unfamiliar audience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Interfaces - TypeScript
How to write an interface with TypeScript. ... The printLabel function has a single parameter that requires that the object passed in has...
Read more >
Interfaces
How to write a method that specifies the type of a parameter using the name of the interface. This includes what arguments -references...
Read more >
Understanding and using interfaces in TypeScript
An interface contains the name of all the properties along with their types. It also includes the signature for functions along with the...
Read more >
Interfaces in Typescript: What are they and how to use them?
Interfaces define the specifications of entities, the types of the objects, and implementation by using functions or classes. We can specify ...
Read more >
How to Pass interface Object as a Parameter? - YouTube
How to Pass interface Object as a Parameter ? | Core Java Interview Questions | Naresh IT** For Online Training Registration: ...
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