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): allow explicit any for no-unsafe-return #3498

Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 10 additions & 7 deletions packages/eslint-plugin/src/rules/no-unsafe-return.ts
Expand Up @@ -91,6 +91,16 @@ export default util.createRule({
functionType = checker.getTypeAtLocation(functionTSNode);
}

// If there is an explicit type annotation *and* that type matches the actual
// 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()) {
return;
}
}
}

if (anyType !== util.AnyType.Safe) {
// Allow cases when the declared return type of the function is either unknown or unknown[]
// and the function is returning any or any[].
Expand Down Expand Up @@ -140,13 +150,6 @@ export default util.createRule({

for (const signature of functionType.getCallSignatures()) {
const functionReturnType = signature.getReturnType();
if (returnNodeType === functionReturnType) {
// don't bother checking if they're the same
// either the function is explicitly declared to return the same type
// or there was no declaration, so the return type is implicit
return;
}

const result = util.isUnsafeAssignment(
returnNodeType,
functionReturnType,
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts
Expand Up @@ -40,6 +40,18 @@ function foo() {
`
function foo() {
return [];
}
`,
// explicit any return type is allowed, if you want to be unsafe like that
`
function foo(): any {
return {} as any;
}
`,
// explicit any array return type is allowed, if you want to be unsafe like that
`
function foo(): any[] {
return [] as any[];
}
`,
// explicit any generic return type is allowed, if you want to be unsafe like that
Expand Down