Skip to content

fix(eslint-plugin): [no-misused-promises] check contextual type #10042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 29, 2024
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
14 changes: 14 additions & 0 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,20 @@ function checkThenableOrVoidArgument(
) {
voidReturnIndices.add(index);
}
const contextualType = checker.getContextualTypeForArgumentAtIndex(
node,
index,
);
if (contextualType !== type) {
checkThenableOrVoidArgument(
checker,
node,
contextualType,
index,
thenableReturnIndices,
voidReturnIndices,
);
}
}

// Get the positions of arguments which are void functions (and not also
Expand Down
107 changes: 107 additions & 0 deletions packages/eslint-plugin/tests/rules/no-misused-promises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,21 @@ interface MyInterface extends MyCall, MyIndex, MyConstruct, MyMethods {
const array: number[] = [1, 2, 3];
array.filter(a => a > 1);
`,
`
type ReturnsPromiseVoid = () => Promise<void>;
declare const useCallback: <T extends (...args: unknown[]) => unknown>(
fn: T,
) => T;
useCallback<ReturnsPromiseVoid>(async () => {});
`,
`
type ReturnsVoid = () => void;
type ReturnsPromiseVoid = () => Promise<void>;
declare const useCallback: <T extends (...args: unknown[]) => unknown>(
fn: T,
) => T;
useCallback<ReturnsVoid | ReturnsPromiseVoid>(async () => {});
`,
],

invalid: [
Expand Down Expand Up @@ -2322,5 +2337,97 @@ tuple.find(() => Promise.resolve(false));
},
],
},
{
code: `
type ReturnsVoid = () => void;
declare const useCallback: <T extends (...args: unknown[]) => unknown>(
fn: T,
) => T;
declare const useCallbackReturningVoid: typeof useCallback<ReturnsVoid>;
useCallbackReturningVoid(async () => {});
`,
errors: [
{
line: 7,
messageId: 'voidReturnArgument',
},
],
},
{
code: `
type ReturnsVoid = () => void;
declare const useCallback: <T extends (...args: unknown[]) => unknown>(
fn: T,
) => T;
useCallback<ReturnsVoid>(async () => {});
`,
errors: [
{
line: 6,
messageId: 'voidReturnArgument',
},
],
},
{
code: `
interface Foo<T> {
(callback: () => T): void;
(callback: () => number): void;
}
declare const foo: Foo<void>;

foo(async () => {});
`,
errors: [
{
line: 8,
messageId: 'voidReturnArgument',
},
],
},
{
code: `
declare function tupleFn<T extends (...args: unknown[]) => unknown>(
...fns: [T, string, T]
): void;
tupleFn<() => void>(
async () => {},
'foo',
async () => {},
);
`,
errors: [
{
line: 6,
messageId: 'voidReturnArgument',
},
{
line: 8,
messageId: 'voidReturnArgument',
},
],
},
{
code: `
declare function arrayFn<T extends (...args: unknown[]) => unknown>(
...fns: (T | string)[]
): void;
arrayFn<() => void>(
async () => {},
'foo',
async () => {},
);
`,
errors: [
{
line: 6,
messageId: 'voidReturnArgument',
},
{
line: 8,
messageId: 'voidReturnArgument',
},
],
},
],
});
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy