Skip to content

Commit

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

Add GetArrayUnion type
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ export type IsNonVoid<Actual> = IsVoid<Actual> extends true ? false : true
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
18 changes: 18 additions & 0 deletions tests/GetArrayUnion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable unused-imports/no-unused-vars */
import type { Equals, Expect, GetArrayUnion } from '..'

// Passes the GetArrayUnion test when the resulting type matches the number array
const testArray = [1, 2, 3]
type Result = Expect<Equals<GetArrayUnion<typeof testArray>, number>>
// ^?

// Passes the GetArrayUnion test when the resulting type matches the array with const
const testArrayConst = [1, 2, 'stringHere'] as const
type ResultConst = Expect<Equals<GetArrayUnion<typeof testArrayConst>, 1 | 2 | 'stringHere'>>
// ^?

// Failed the GetArrayUnion test when the resulting type does equal the expectation
const testArrayFail = [1, 2, 'stringHere'] as const
// @ts-expect-error - GetParameters failing the equality checker
type ResultFailure = Expect<Equals<GetParameters<typeof testArrayFail>, number | string>>
// ^?

0 comments on commit 9538b47

Please sign in to comment.