Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid non-promise types in @return on async functions #1345

Open
Daimona opened this issue Dec 14, 2024 · 1 comment
Open

Forbid non-promise types in @return on async functions #1345

Daimona opened this issue Dec 14, 2024 · 1 comment

Comments

@Daimona
Copy link

Daimona commented Dec 14, 2024

Motivation

async functions always return promises, and can never return anything else. Therefore, documenting an async function as @return {T} is always wrong, unless T is (a subtype of) Promise.

Current behavior

The existing rules do not flag those return tags as incorrect. This problem was also mentioned in #452 (comment).

Desired behavior

I would like to receive a warning if an async function is annotated as returning a non-promise. This could be part of an existing rule (presumably in the require-return* family) or a new rule. Either would work.

Alternatives considered

If checking for non-promises in general is too complicated, it would be great to at least check native types for the time being. So warn for @return {string}, @return {void} etc., but ignore @return {SomeCustomType}.

@juhort
Copy link

juhort commented Dec 31, 2024

@Daimona Curious what your workflow is? I'm guessing you've js files with JSDoc comments and you're using TS to check the JSDoc comments.

AFAIU, the require-return* rules don't check if the types in JSDoc align with the types in the implementation. For eg., the following snippet has no complains from ESLint, although the return types clearly don't match.

/**
 *
 * @param {number} a - First number
 * @param {number} b - Second number
 * @returns {number} - Sum of a and b
 */
function add(a, b) {
    const result = a + b;
    return result.toString(); // Typed as `number` in JSDoc, but returns a `string`
}

const result = add(1, 2);
//    ^? const result: number

But if you've TS checking your js files, then TS would complain:
image

And TS would also complain if the return type for an async function is not wrapped in Promise, like:
image

So, this plugin is anyways not doing any type checking, it's just validating the presence of certain tags and description in JSDoc comments. Am I missing something?? Probably there's some other workflow that I'm not aware of, but I'm happy to learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants