Skip to content

Commit abe4c38

Browse files
fix(no-misused-observables): accept Observable-returning function for union types.
1 parent 73da801 commit abe4c38

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/rules/no-misused-observables.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isJSXExpressionContainer,
1010
isMethodDefinition,
1111
isPropertyDefinition,
12+
couldBeType as tsutilsEtcCouldBeType,
1213
} from '../etc';
1314
import { ruleCreator } from '../utils';
1415

@@ -390,6 +391,12 @@ function isVoidReturningFunctionType(
390391
for (const signature of subType.getCallSignatures()) {
391392
const returnType = signature.getReturnType();
392393

394+
// If a certain positional argument accepts both Observable and void returns,
395+
// an Observable-returning function is valid.
396+
if (tsutilsEtcCouldBeType(returnType, 'Observable')) {
397+
return false;
398+
}
399+
393400
hasVoidReturn ||= tsutils.isTypeFlagSet(returnType, ts.TypeFlags.Void);
394401
}
395402
}

tests/rules/no-misused-observables.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,18 @@ ruleTester({ types: true }).run('no-misused-observables', noMisusedObservablesRu
197197
bar,
198198
};
199199
`,
200+
stripIndent`
201+
// void return property; union type
202+
import { Observable, of } from "rxjs";
203+
204+
interface Hook {
205+
onInit?: ((field: string) => void) | ((field: string) => Observable<any>);
206+
}
207+
208+
const hook: Hook = {
209+
onInit: field => of(field),
210+
};
211+
`,
200212
// #endregion valid; void return property
201213
// #region valid; void return return value
202214
{

0 commit comments

Comments
 (0)