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

Skip to content

Commit b15a2b2

Browse files
author
Josh Goldberg
authored
fix(eslint-plugin): allow explicit any for no-unsafe-return (typescript-eslint#3498)
1 parent 288092a commit b15a2b2

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

packages/eslint-plugin/src/rules/no-unsafe-return.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ export default util.createRule({
9191
functionType = checker.getTypeAtLocation(functionTSNode);
9292
}
9393

94+
// If there is an explicit type annotation *and* that type matches the actual
95+
// function return type, we shouldn't complain (it's intentional, even if unsafe)
96+
if (functionTSNode.type) {
97+
for (const signature of functionType.getCallSignatures()) {
98+
if (returnNodeType === signature.getReturnType()) {
99+
return;
100+
}
101+
}
102+
}
103+
94104
if (anyType !== util.AnyType.Safe) {
95105
// Allow cases when the declared return type of the function is either unknown or unknown[]
96106
// and the function is returning any or any[].
@@ -140,13 +150,6 @@ export default util.createRule({
140150

141151
for (const signature of functionType.getCallSignatures()) {
142152
const functionReturnType = signature.getReturnType();
143-
if (returnNodeType === functionReturnType) {
144-
// don't bother checking if they're the same
145-
// either the function is explicitly declared to return the same type
146-
// or there was no declaration, so the return type is implicit
147-
return;
148-
}
149-
150153
const result = util.isUnsafeAssignment(
151154
returnNodeType,
152155
functionReturnType,

packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ function foo() {
4040
`
4141
function foo() {
4242
return [];
43+
}
44+
`,
45+
// explicit any return type is allowed, if you want to be unsafe like that
46+
`
47+
function foo(): any {
48+
return {} as any;
49+
}
50+
`,
51+
// explicit any array return type is allowed, if you want to be unsafe like that
52+
`
53+
function foo(): any[] {
54+
return [] as any[];
4355
}
4456
`,
4557
// explicit any generic return type is allowed, if you want to be unsafe like that

0 commit comments

Comments
 (0)