From 7b59e10a8305acc4c31f31fa9ca063b94d860159 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 23 Apr 2024 16:13:32 +0900 Subject: [PATCH] feat: replace `any` with `unknown` for default types --- packages/spy/src/index.ts | 10 +++++----- test/core/test/spy.test.ts | 2 +- test/core/test/vi.spec.ts | 13 +++++++++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/spy/src/index.ts b/packages/spy/src/index.ts index db22a60ccd3a..a9dfcc70fe04 100644 --- a/packages/spy/src/index.ts +++ b/packages/spy/src/index.ts @@ -92,8 +92,8 @@ export interface MockContext { lastCall: Parameters | undefined } -// TODO: use `(...args: unknown[]) => unknown` for stricter default like jest? type Procedure = (...args: any[]) => any +type UnknownProcedure = (...args: unknown[]) => unknown type Methods = keyof { [K in keyof T as T[K] extends Procedure ? K : never]: T[K]; @@ -117,7 +117,7 @@ Jest uses the latter for `MockInstance.mockImplementation` etc... and it allows const boolFn: Jest.Mock<() => boolean> = jest.fn<() => true>(() => true) */ /* eslint-disable ts/method-signature-style */ -export interface MockInstance { +export interface MockInstance { /** * Use it to return the name given to mock with method `.mockName(name)`. */ @@ -249,14 +249,14 @@ export interface MockInstance { } /* eslint-enable ts/method-signature-style */ -export interface Mock extends MockInstance { +export interface Mock extends MockInstance { new (...args: Parameters): ReturnType (...args: Parameters): ReturnType } type PartialMaybePromise = T extends Promise> ? Promise>> : Partial -export interface PartialMock extends MockInstance<(...args: Parameters) => PartialMaybePromise>> { +export interface PartialMock extends MockInstance<(...args: Parameters) => PartialMaybePromise>> { new (...args: Parameters): ReturnType (...args: Parameters): ReturnType } @@ -514,7 +514,7 @@ function enhanceSpy( return stub as any } -export function fn( +export function fn( implementation?: T, ): Mock { const enhancedSpy = enhanceSpy(tinyspy.internalSpyOn({ spy: implementation || (() => {}) }, 'spy')) diff --git a/test/core/test/spy.test.ts b/test/core/test/spy.test.ts index 35cddb90374a..719160540b01 100644 --- a/test/core/test/spy.test.ts +++ b/test/core/test/spy.test.ts @@ -15,7 +15,7 @@ describe('spyOn', () => { test('infers a class correctly', () => { vi.spyOn(mock, 'HelloWorld').mockImplementationOnce(() => { - const Mock = vi.fn() + const Mock = vi.fn() Mock.prototype.hello = vi.fn(() => 'hello world') return new Mock() }) diff --git a/test/core/test/vi.spec.ts b/test/core/test/vi.spec.ts index 86cacc412462..5de26f05f495 100644 --- a/test/core/test/vi.spec.ts +++ b/test/core/test/vi.spec.ts @@ -41,6 +41,15 @@ describe('testing vi utils', () => { }) expectType boolean>>(vi.fn(() => true)) expectType boolean>>(vi.fn()) + + expectType boolean>>(vi.fn<() => boolean>(() => true)) + expectType boolean>>(vi.fn<() => boolean>(() => true)) + expectType<() => boolean>(vi.fn(() => true)) + + // @ts-expect-error default unkonwn + expectType<(v: number) => boolean>(vi.fn()) + + expectType<(v: number) => boolean>(vi.fn()) }) test('vi partial mocked', () => { @@ -57,7 +66,7 @@ describe('testing vi utils', () => { }) vi.mocked(mockFactory, { partial: true, deep: false }).mockReturnValue({ - bar: vi.fn(), + bar: vi.fn(), }) vi.mocked(mockFactory, { partial: true, deep: true }).mockReturnValue({ @@ -71,7 +80,7 @@ describe('testing vi utils', () => { }) vi.mocked(mockFactoryAsync, { partial: true, deep: false }).mockResolvedValue({ - bar: vi.fn(), + bar: vi.fn(), }) vi.mocked(mockFactoryAsync, { partial: true, deep: true }).mockResolvedValue({