From 4bfbc6b31a700abb062c23cc09c6efcce109a278 Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Tue, 31 Jan 2023 14:19:08 -0800 Subject: [PATCH] feat(@jest/expect): Expose type of actual to Matchers Matchers isn't as typed as some users would like (see #13334, #13812). For users who want to customize it by extending the `Matchers` interface, it's useful to have access to the type of `actual` (the argument of `expect`) so you can do, say, ```ts interface Matchers { toTypedEqual(expected: T): R } ``` This commit exposes it. The first-party matchers still have the same types as before. --- CHANGELOG.md | 2 ++ packages/expect/src/types.ts | 2 +- packages/jest-expect/src/types.ts | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4997b655331c..5ac9e1680043 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[@jest/expect]` provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions (#TBD) + ### Fixes ### Chore & Maintenance diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index f3687b5597cb..3803d1e251b4 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -134,7 +134,7 @@ type PromiseMatchers = { resolves: Matchers> & Inverse>>; }; -export interface Matchers> { +export interface Matchers, T = unknown> { /** * Ensures the last call to a mock function was provided specific args. */ diff --git a/packages/jest-expect/src/types.ts b/packages/jest-expect/src/types.ts index 10739da8481e..d1ba64a7f66f 100644 --- a/packages/jest-expect/src/types.ts +++ b/packages/jest-expect/src/types.ts @@ -29,7 +29,7 @@ type Inverse = { not: Matchers; }; -type JestMatchers, T> = Matchers & +type JestMatchers, T> = Matchers & SnapshotMatchers; type PromiseMatchers = {