mirror of
https://github.com/sindresorhus/type-fest.git
synced 2025-12-08 19:25:05 +00:00
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
/**
|
|
Returns a boolean for whether the two given types are equal.
|
|
|
|
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
*/
|
|
export type IsEqual<T, U> =
|
|
(<G>() => G extends T ? 1 : 2) extends
|
|
(<G>() => G extends U ? 1 : 2)
|
|
? true
|
|
: false;
|
|
|
|
/**
|
|
Infer the length of the given array `<T>`.
|
|
|
|
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
*/
|
|
type TupleLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never;
|
|
|
|
/**
|
|
Create a tuple type of the given length `<L>`.
|
|
|
|
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
*/
|
|
type BuildTuple<L extends number, T extends readonly unknown[] = []> = T extends {readonly length: L}
|
|
? T
|
|
: BuildTuple<L, [...T, unknown]>;
|
|
|
|
/**
|
|
Create a tuple of length `A` and a tuple composed of two other tuples,
|
|
the inferred tuple `U` and a tuple of length `B`, then extracts the length of tuple `U`.
|
|
|
|
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
*/
|
|
export type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...(infer U), ...BuildTuple<B>]
|
|
? TupleLength<U>
|
|
: never;
|