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>> +// ^?