File tree Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,16 @@ export default util.createRule({
91
91
functionType = checker . getTypeAtLocation ( functionTSNode ) ;
92
92
}
93
93
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
+
94
104
if ( anyType !== util . AnyType . Safe ) {
95
105
// Allow cases when the declared return type of the function is either unknown or unknown[]
96
106
// and the function is returning any or any[].
@@ -140,13 +150,6 @@ export default util.createRule({
140
150
141
151
for ( const signature of functionType . getCallSignatures ( ) ) {
142
152
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
-
150
153
const result = util . isUnsafeAssignment (
151
154
returnNodeType ,
152
155
functionReturnType ,
Original file line number Diff line number Diff line change @@ -40,6 +40,18 @@ function foo() {
40
40
`
41
41
function foo() {
42
42
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[];
43
55
}
44
56
` ,
45
57
// explicit any generic return type is allowed, if you want to be unsafe like that
You can’t perform that action at this time.
0 commit comments