Skip to content

Commit 93c6dab

Browse files
fix(no-misused-observables): accept Observable-returning function for union types (#236)
Allow returning Observables from union types of properties like: ```ts interface Hook { onInit?: ((field: string) => void) | ((field: string) => Observable<any>); } const hook: Hook = { onInit: field => of(field), }; ``` This corner case was skipped when the initial rule was created based on tseslint's `no-misused-promises`. Resolves #193
1 parent 1e6e33b commit 93c6dab

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
@@ -11,6 +11,7 @@ import {
1111
isJSXExpressionContainer,
1212
isMethodDefinition,
1313
isPropertyDefinition,
14+
couldBeType as tsutilsEtcCouldBeType,
1415
} from '../etc';
1516
import { ruleCreator } from '../utils';
1617

@@ -418,6 +419,12 @@ function isVoidReturningFunctionType(
418419
for (const signature of subType.getCallSignatures()) {
419420
const returnType = signature.getReturnType();
420421

422+
// If a certain positional argument accepts both Observable and void returns,
423+
// an Observable-returning function is valid.
424+
if (tsutilsEtcCouldBeType(returnType, 'Observable')) {
425+
return false;
426+
}
427+
421428
hasVoidReturn ||= tsutils.isTypeFlagSet(returnType, ts.TypeFlags.Void);
422429
}
423430
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ ruleTester({ types: true }).run('no-misused-observables', noMisusedObservablesRu
233233
bar,
234234
};
235235
`,
236+
stripIndent`
237+
// void return property; union type
238+
import { Observable, of } from "rxjs";
239+
240+
interface Hook {
241+
onInit?: ((field: string) => void) | ((field: string) => Observable<any>);
242+
}
243+
244+
const hook: Hook = {
245+
onInit: field => of(field),
246+
};
247+
`,
236248
// #endregion valid; void return property
237249
// #region valid; void return return value
238250
{

0 commit comments

Comments
 (0)