From e57f22ac9b140b961cb304bd4ab788bf4ce1c1e1 Mon Sep 17 00:00:00 2001 From: CRBroughton Date: Wed, 23 Oct 2024 21:21:09 +0100 Subject: [PATCH] feat: :sparkles: Add TupleElementAt --- .changeset/fresh-tigers-pull.md | 5 +++++ index.ts | 2 ++ tests/TupleElementAt.test.ts | 11 +++++++++++ 3 files changed, 18 insertions(+) create mode 100644 .changeset/fresh-tigers-pull.md create mode 100644 tests/TupleElementAt.test.ts diff --git a/.changeset/fresh-tigers-pull.md b/.changeset/fresh-tigers-pull.md new file mode 100644 index 0000000..9c0835c --- /dev/null +++ b/.changeset/fresh-tigers-pull.md @@ -0,0 +1,5 @@ +--- +"@crbroughton/ts-test-utils": minor +--- + +Add TupleElementAt diff --git a/index.ts b/index.ts index d856fcd..5da94d9 100644 --- a/index.ts +++ b/index.ts @@ -61,6 +61,8 @@ export type GetParameters = T extends (...args: infer P) => any ? P : never export type GetArrayUnion = T extends readonly (infer U)[] ? U : never +export type TupleElementAt = T[Index] + export type DeepReturnType = T extends (...args: unknown[]) => infer ReturnType ? ReturnType extends (...args: unknown[]) => unknown diff --git a/tests/TupleElementAt.test.ts b/tests/TupleElementAt.test.ts new file mode 100644 index 0000000..429f4d0 --- /dev/null +++ b/tests/TupleElementAt.test.ts @@ -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, 'b'>> +// ^? + +// Failed the TupleElementAt test - non-narrowed to the literal string +// @ts-expect-error - Fails the exclusion +type ResultFailure = Expect, string>> +// ^?