Skip to content

Commit

Permalink
fix(eslint-plugin): [no-misused-promises] improve check union types (#…
Browse files Browse the repository at this point in the history
…8534)

* fix(eslint-plugin): [no-misused-promises] improve check union types

* apply reviews
  • Loading branch information
yeonjuan committed Feb 23, 2024
1 parent c1441c8 commit 1458920
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
9 changes: 3 additions & 6 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Expand Up @@ -674,10 +674,7 @@ function isVoidReturningFunctionType(
*/
function returnsThenable(checker: ts.TypeChecker, node: ts.Node): boolean {
const type = checker.getApparentType(checker.getTypeAtLocation(node));

if (anySignatureIsThenableType(checker, node, type)) {
return true;
}

return false;
return tsutils
.unionTypeParts(type)
.some(t => anySignatureIsThenableType(checker, node, t));
}
21 changes: 21 additions & 0 deletions packages/eslint-plugin/tests/rules/no-misused-promises.test.ts
Expand Up @@ -474,6 +474,11 @@ restTuple('Hello');
};
}
`,
`
declare function foo(cb: undefined | (() => void));
declare const bar: undefined | (() => void);
foo(bar);
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/6637
{
code: `
Expand Down Expand Up @@ -1227,5 +1232,21 @@ const test: ReturnsRecord = () => {
`,
errors: [{ line: 7, messageId: 'voidReturnProperty' }],
},
{
code: `
declare function foo(cb: undefined | (() => void));
declare const bar: undefined | (() => Promise<void>);
foo(bar);
`,
errors: [{ line: 4, messageId: 'voidReturnArgument' }],
},
{
code: `
declare function foo(cb: string & (() => void));
declare const bar: string & (() => Promise<void>);
foo(bar);
`,
errors: [{ line: 4, messageId: 'voidReturnArgument' }],
},
],
});

0 comments on commit 1458920

Please sign in to comment.