[fabric-core] Add basic array-element utility types

This commit is contained in:
Pablo Baleztena 2024-09-04 20:08:34 -03:00
parent 0ffe2838c1
commit c4483f073e
4 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,12 @@
import { describe, expectTypeOf, test } from "vitest";
describe("ArrayElement utils", () => {
test("Given an array, it should return the element type of the array", () => {
type ArrayElement<T extends readonly unknown[]> =
T extends readonly (infer U)[] ? U : never;
type result = ArrayElement<["a", "b", "c"]>;
expectTypeOf<result>().toEqualTypeOf<"a" | "b" | "c">();
});
});

View File

@ -0,0 +1,17 @@
/**
* Get the element type of an array.
*/
export type ArrayElement<T extends readonly unknown[]> =
T extends readonly (infer U)[] ? U : never;
/**
* Get the first element type of a tuple.
*/
export type TupleFirstElement<T extends readonly unknown[]> =
T extends readonly [infer U, ...unknown[]] ? U : never;
/**
* Get the LAST element type of a tuple.
*/
export type TupleLastElement<T extends readonly unknown[]> =
T extends readonly [...unknown[], infer U] ? U : never;

View File

@ -0,0 +1 @@
export * from "./array-element.js";

View File

@ -1,3 +1,4 @@
export * from "./array/index.js";
export * from "./domain/index.js";
export * from "./error/index.js";
export * from "./record/index.js";