From ff6e32f131f0ff8b7dc84d1bb2ddf109ada3205c Mon Sep 17 00:00:00 2001 From: CRBroughton Date: Tue, 22 Oct 2024 21:00:36 +0100 Subject: [PATCH] feat: :sparkles: Add DeepReturnType --- .changeset/wet-pens-worry.md | 5 +++++ index.ts | 7 +++++++ tests/DeepReturnType.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 .changeset/wet-pens-worry.md create mode 100644 tests/DeepReturnType.test.ts diff --git a/.changeset/wet-pens-worry.md b/.changeset/wet-pens-worry.md new file mode 100644 index 0000000..a2f8e0d --- /dev/null +++ b/.changeset/wet-pens-worry.md @@ -0,0 +1,5 @@ +--- +"@crbroughton/ts-test-utils": minor +--- + +Add DeepReturnType diff --git a/index.ts b/index.ts index a1c810e..d856fcd 100644 --- a/index.ts +++ b/index.ts @@ -60,3 +60,10 @@ export type NonVoid = Actual extends void ? never : Actual export type GetParameters = T extends (...args: infer P) => any ? P : never export type GetArrayUnion = T extends readonly (infer U)[] ? U : never + +export type DeepReturnType = + T extends (...args: unknown[]) => infer ReturnType + ? ReturnType extends (...args: unknown[]) => unknown + ? DeepReturnType + : ReturnType + : never diff --git a/tests/DeepReturnType.test.ts b/tests/DeepReturnType.test.ts new file mode 100644 index 0000000..27a7233 --- /dev/null +++ b/tests/DeepReturnType.test.ts @@ -0,0 +1,22 @@ +/* eslint-disable unused-imports/no-unused-vars */ +import type { DeepReturnType, Equals, Expect } from '..' + +function multiLevelFunction() { + return () => { + return () => { + return () => { + return 42 + } + return false + } + return 'hello' + } +} + +type Result = Expect, number | false | 'hello'>> +// ^? + +// Failed the DeepReturnType test when the resulting type does equal the expectation +// @ts-expect-error - GetParameters failing the equality checker +type ResultFailure = Expect, number | false | string>> +// ^?