Search code examples
javascriptjsdoc

How to declare type of destructured function argument *within function call* in JSDoc?


When using Playwright, tests are specified within a function argument of the test function, taking optional fixtures in the nested function's argument, like so:

// `page` is fixture
test("some test", async ({ page }) => {
    await page.goto("https://google.com");
});

I am using a Page-Object Model pattern, so I'd like to be able to annotate a custom fixture object with its type:

// In TS, this would be as simple as: `pomPage: SomeClass`
test("some test", async ({ pomPage }) => {
    await pomPage.goto();
});

How can I accomplish this with a JSDoc type annotation? There are several SO answers about annotating destructured function arguments, but none of them seem to address the case where the function whose argument should be annotated is itself an argument to another function call.

Note: This question should not receive answers whose basic form reduces to "use TypeScript"—it's specifically about JSDoc.


Solution

  • You can add the JSDoc comment directly above the anonymous function.

    test("some test", 
        /**
         * @param {{
         *  page: SomeType
         * }} param0
         */
        async ({ page }) => {
            await page.goto("https://google.com");
        }
    );