Skip to content

Commit

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

Add TupleElementAt
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ 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 TupleElementAt<T extends unknown[], Index extends number> = T[Index]

export type DeepReturnType<T> =
T extends (...args: unknown[]) => infer ReturnType
? ReturnType extends (...args: unknown[]) => unknown
Expand Down
11 changes: 11 additions & 0 deletions tests/TupleElementAt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable unused-imports/no-unused-vars */
import type { Equals, Expect, TupleElementAt } from '..'

// Passes the assignable test when the first type is assignable
type Result = Expect<Equals<TupleElementAt<['a', 'b', 'c'], 1>, 'b'>>
// ^?

// Failed the TupleElementAt test - non-narrowed to the literal string
// @ts-expect-error - Fails the exclusion
type ResultFailure = Expect<Equals<TupleElementAt<['a', 'b', 'c'], 1>, string>>
// ^?

0 comments on commit e57f22a

Please sign in to comment.