-
-
Notifications
You must be signed in to change notification settings - Fork 573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose ArrayElement
type
#914
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
Extracts the element type of an array or union of arrays. | ||
|
||
Returns `never` if T is not an array. | ||
|
||
It creates a type-safe way to access the element type of `unknown` type. | ||
|
||
@example | ||
``` | ||
import type {ArrayElement} from 'type-fest'; | ||
|
||
declare const getMostCommonElement: <T>(array: T[]) => ArrayElement<typeof array>; | ||
|
||
getMostCommonElement([1, 2, 3]); | ||
//=> 1 | 2 | 3 | ||
|
||
getMostCommonElement(['foo', 'bar', 'baz'] as const); | ||
//=> 'foo' | 'bar' | 'baz' | ||
``` | ||
|
||
@see {@link ArrayValues} for when you know that the input is an array. | ||
|
||
@category Array | ||
*/ | ||
export type ArrayElement<ArrayType extends readonly unknown[]> = | ||
ArrayType extends ReadonlyArray<infer ElementType> | ||
? ElementType | ||
: never; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {expectType} from 'tsd'; | ||
import type {ArrayElement} from '../index'; | ||
|
||
declare const getArrayElement: <T extends readonly unknown[]>(array: T) => ArrayElement<T>; | ||
|
||
expectType<string>(getArrayElement(['a', 'b', 'c'])); | ||
expectType<'a' | 'b' | 'c'>(getArrayElement(['a', 'b', 'c'] as const)); | ||
expectType<number>(getArrayElement([1, 2, 3])); | ||
expectType<string | number>(getArrayElement(['a', 1])); | ||
|
||
declare const notArray: ArrayElement<unknown>; | ||
Check failure on line 11 in test-d/array-element.ts GitHub Actions / TypeScript ~5.1.0
Check failure on line 11 in test-d/array-element.ts GitHub Actions / TypeScript latest
Check failure on line 11 in test-d/array-element.ts GitHub Actions / TypeScript ~5.2.0
Check failure on line 11 in test-d/array-element.ts GitHub Actions / TypeScript ~5.3.0
Check failure on line 11 in test-d/array-element.ts GitHub Actions / TypeScript ~5.4.0
Check failure on line 11 in test-d/array-element.ts GitHub Actions / Node.js 16
Check failure on line 11 in test-d/array-element.ts GitHub Actions / Node.js 20
|
||
|
||
expectType<never>(getArrayElement([])); | ||
expectType<never>(notArray); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this works @strongpauly
// #906 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, as long as it returns a union of the types of the array elements, its fit for purpose, IMO.