[fabric-core] Add basic array-element utility types
This commit is contained in:
parent
0ffe2838c1
commit
c4483f073e
12
packages/fabric/core/src/array/array-element.spec.ts
Normal file
12
packages/fabric/core/src/array/array-element.spec.ts
Normal 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">();
|
||||
});
|
||||
});
|
||||
17
packages/fabric/core/src/array/array-element.ts
Normal file
17
packages/fabric/core/src/array/array-element.ts
Normal 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;
|
||||
1
packages/fabric/core/src/array/index.ts
Normal file
1
packages/fabric/core/src/array/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./array-element.js";
|
||||
@ -1,3 +1,4 @@
|
||||
export * from "./array/index.js";
|
||||
export * from "./domain/index.js";
|
||||
export * from "./error/index.js";
|
||||
export * from "./record/index.js";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user