Skip to content

Commit

Permalink
feat: ✨ Add DeepReturnType
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBroughton committed Oct 22, 2024
1 parent 9538b47 commit ff6e32f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-pens-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crbroughton/ts-test-utils": minor
---

Add DeepReturnType
7 changes: 7 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ export type NonVoid<Actual> = Actual extends void ? never : Actual
export type GetParameters<T> = T extends (...args: infer P) => any ? P : never

export type GetArrayUnion<T> = T extends readonly (infer U)[] ? U : never

export type DeepReturnType<T> =
T extends (...args: unknown[]) => infer ReturnType
? ReturnType extends (...args: unknown[]) => unknown
? DeepReturnType<ReturnType>
: ReturnType
: never
22 changes: 22 additions & 0 deletions tests/DeepReturnType.test.ts
Original file line number Diff line number Diff line change
@@ -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<Equals<DeepReturnType<typeof multiLevelFunction>, 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<Equals<DeepReturnType<typeof multiLevelFunction>, number | false | string>>
// ^?

0 comments on commit ff6e32f

Please sign in to comment.