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

fix(eslint-plugin): [no-unsafe-return] allow returning anything if explicitly returning any #7708

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/eslint-plugin/src/rules/no-unsafe-return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ export default util.createRule({
// function return type, we shouldn't complain (it's intentional, even if unsafe)
if (functionTSNode.type) {
for (const signature of functionType.getCallSignatures()) {
if (returnNodeType === signature.getReturnType()) {
if (
returnNodeType === signature.getReturnType() ||
util.isTypeFlagSet(
signature.getReturnType(),
ts.TypeFlags.Any | ts.TypeFlags.Unknown,
)
) {
return;
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ function foo(): Set<number> {
return new Map();
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/3549
`
function foo(): any {
return [] as any[];
}
`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Testing] Could you add a test for unknown too?

some room for interpretation.

Yeah I think this is fine as-is for now. If folks really give solid use-cases for the more complex ones we can always look into adding more complexity.

],
invalid: [
{
Expand Down