Skip to content
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

Improve waitFor() types #459

Merged
merged 1 commit into from
Sep 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions addon/@ember/test-waiters/wait-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ type DecoratorArguments = [object, string, PropertyDescriptor, string?];
* }
*
*/
export default function waitFor(fn: AsyncFunction<any[], any>, label?: string): Function;
export default function waitFor(
fn: CoroutineFunction<any[], any>,
export default function waitFor<A extends Array<any>, PromiseReturn>(
fn: AsyncFunction<A, PromiseReturn>,
label?: string
): AsyncFunction<A, PromiseReturn>;
export default function waitFor<A extends Array<any>, T>(
fn: CoroutineFunction<A, T>,
label?: string
): CoroutineFunction<any[], any>;
): CoroutineFunction<A, T>;
export default function waitFor(
target: object,
_key: string,
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/wait-for-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,32 @@ if (DEBUG) {
assert.deepEqual(getPendingWaiterState().pending, 0);
});
});

test('types', async function (assert) {
assert.expect(0);

async function asyncFn(a: string, b: string) {
return `${a}${b}`;
}
function* genFn(a: string, b: string) {
yield `${a}${b}`;
return `${a}${b}`;
}

function asyncNoop(fn: typeof asyncFn) {
return fn;
}
function genNoop(fn: typeof genFn) {
return fn;
}

asyncNoop(waitFor(asyncFn));
genNoop(waitFor(genFn));

// @ts-expect-error wrong argument types
waitFor(asyncFn)(1, 2);
// @ts-expect-error wrong argument types
waitFor(genFn)(1, 2);
});
});
}