From 40809501ad2bc04c47393573b2bcfb27080793ef Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 16:45:34 +0300 Subject: [PATCH] refactor: use idea from pr #13278 --- packages/expect/src/types.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 9301530db8df..b9c0337e208e 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -118,13 +118,6 @@ export interface AsymmetricMatchers { stringMatching(sample: string | RegExp): AsymmetricMatcher; } -// if T is a function, return its parameters array type, otherwise return an unknown array type -type ConditionalFunctionParameters = T extends ( - ...args: Array -) => unknown - ? Parameters - : Array; - type PromiseMatchers = { /** * Unwraps the reason of a rejected promise so any other matcher can be chained. @@ -138,11 +131,15 @@ type PromiseMatchers = { resolves: Matchers> & Inverse, T>>; }; +type EnsureFunctionLike = T extends (...args: Array) => unknown + ? T + : never; + export interface Matchers, T = unknown> { /** * Ensures the last call to a mock function was provided specific args. */ - lastCalledWith(...expected: ConditionalFunctionParameters): R; + lastCalledWith(...expected: Parameters>): R; /** * Ensure that the last call to a mock function has returned a specified value. */ @@ -150,7 +147,7 @@ export interface Matchers, T = unknown> { /** * Ensure that a mock function is called with specific arguments on an Nth call. */ - nthCalledWith(nth: number, ...expected: ConditionalFunctionParameters): R; + nthCalledWith(nth: number, ...expected: Parameters>): R; /** * Ensure that the nth call to a mock function has returned a specified value. */ @@ -171,7 +168,7 @@ export interface Matchers, T = unknown> { /** * Ensure that a mock function is called with specific arguments. */ - toBeCalledWith(...expected: ConditionalFunctionParameters): R; + toBeCalledWith(...expected: Parameters>): R; /** * Using exact equality with floating point numbers is a bad idea. * Rounding means that intuitive things fail. @@ -254,19 +251,19 @@ export interface Matchers, T = unknown> { /** * Ensure that a mock function is called with specific arguments. */ - toHaveBeenCalledWith(...expected: ConditionalFunctionParameters): R; + toHaveBeenCalledWith(...expected: Parameters>): R; /** * Ensure that a mock function is called with specific arguments on an Nth call. */ toHaveBeenNthCalledWith( nth: number, - ...expected: ConditionalFunctionParameters + ...expected: Parameters> ): R; /** * If you have a mock function, you can use `.toHaveBeenLastCalledWith` * to test what arguments it was last called with. */ - toHaveBeenLastCalledWith(...expected: ConditionalFunctionParameters): R; + toHaveBeenLastCalledWith(...expected: Parameters>): R; /** * Use to test the specific value that a mock function last returned. * If the last call to the mock function threw an error, then this matcher will fail