Skip to content

Commit a5ff0b7

Browse files
committed
Improve typing
1 parent 5054078 commit a5ff0b7

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,11 @@ export type Mock<TFunc extends AnyMockable> = TFunc extends AnyConstructor
8282
: TFunc extends AnyFunction
8383
? MockedFunction<TFunc>
8484
: never
85+
86+
/** Produce a union of progressively longer tuples based on the input tuple */
87+
export type PartialArguments<
88+
Tuple extends any[],
89+
_Previous extends any[] = [],
90+
> = Tuple extends [infer First, ...infer Rest]
91+
? [..._Previous, First] | PartialArguments<Rest, [..._Previous, First]>
92+
: never

src/vitest-when.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
MockInstance,
99
NormalizeMockable,
1010
ParametersOf,
11+
PartialArguments,
1112
ReturnTypeOf,
1213
WithMatchers,
1314
} from './types.ts'
@@ -24,7 +25,7 @@ export interface StubWrapper<TFunc extends AnyMockable> {
2425

2526
export interface StubWrapperFlexible<TFunc extends AnyMockable> {
2627
calledWith<TArgs extends ParametersOf<TFunc>>(
27-
...args: Partial<WithMatchers<TArgs>>
28+
...args: PartialArguments<WithMatchers<TArgs>>
2829
): Stub<TFunc>
2930
}
3031

test/typing.test-d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ describe('vitest-when type signatures', () => {
5757
>()
5858
})
5959

60+
it('should ensure correct type of previous arguments', () => {
61+
subject
62+
.when(extraArguments, { ignoreExtraArgs: true })
63+
/* @ts-expect-error: first arg is not correct */
64+
.calledWith(undefined, 2)
65+
})
66+
6067
it('returns mock type for then resolve', () => {
6168
const result = subject.when(simpleAsync).calledWith(1).thenResolve('hello')
6269

0 commit comments

Comments
 (0)