Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Enhancement: [no-unsafe-return] Disallow return Promise<any> #8674

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

Closed
4 tasks done
yeonjuan opened this issue Mar 14, 2024 · 1 comment Β· Fixed by #8693
Closed
4 tasks done

Enhancement: [no-unsafe-return] Disallow return Promise<any> #8674

yeonjuan opened this issue Mar 14, 2024 · 1 comment Β· Fixed by #8693
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@yeonjuan
Copy link
Contributor

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-unsafe-return

Description

This proposal is to make no-unsafe-return disallow Promise<any> as well.
playground

declare const promise: Promise<any>;
const bar = async () => await promise; // Error (ok)
const baz = async () => promise; //  Currently, it does not report errors.

https://typescript-eslint.io/rules/no-unsafe-return

Despite your best intentions, the any type can sometimes leak into your codebase. Returning an an any-typed value from a function creates a potential type safety hole and source of bugs in your codebase.

If a Promise<any> value is returned, the any type can leak into your code.

declare const promise: Promise<any>;
const baz = async () => promise;

function addOne(num: number) {
  return num + 1;
}

(async () => {
  addOne(await baz()); // no type error
});

Fail

declare const promise: Promise<any>;

const foo = () => promise;
const bar = async () => promise;
// ...

Pass

declare const promise: Promise<number>;

const foo = () => promise;

Additional Info

No response

@yeonjuan yeonjuan added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 14, 2024
@bradzacher
Copy link
Member

The rule doesn't check T<any> implicit return types at all - it only checks T<any> when the function is explicitly annotated as returning T<U>.

But given Promise's special handling with async functions it probably makes sense to check it the same as with a raw any because the implied return type of an async function is Promise<T>, not T.

I don't think we should add special handling to the non-async usecase, however. I don't think it's right to add special meaning to promise in non-async contexts.

@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Mar 15, 2024
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Aug 20, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants