Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
    }
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading