Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/rules/no-misused-observables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isJSXExpressionContainer,
isMethodDefinition,
isPropertyDefinition,
couldBeType as tsutilsEtcCouldBeType,
} from '../etc';
import { ruleCreator } from '../utils';

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

// If a certain positional argument accepts both Observable and void returns,
// an Observable-returning function is valid.
if (tsutilsEtcCouldBeType(returnType, 'Observable')) {
return false;
}

hasVoidReturn ||= tsutils.isTypeFlagSet(returnType, ts.TypeFlags.Void);
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/rules/no-misused-observables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ ruleTester({ types: true }).run('no-misused-observables', noMisusedObservablesRu
bar,
};
`,
stripIndent`
// void return property; union type
import { Observable, of } from "rxjs";

interface Hook {
onInit?: ((field: string) => void) | ((field: string) => Observable<any>);
}

const hook: Hook = {
onInit: field => of(field),
};
`,
// #endregion valid; void return property
// #region valid; void return return value
{
Expand Down