rivo
Version:
🤖 The ultimate library you need for composable type-level programming in TypeScript, powered by HKT.
39 lines (36 loc) • 1.09 kB
TypeScript
import type { Ary } from ".";
import type { Args, Fn } from "../HKT";
import type { Nat } from "../Num";
import type { Extends, If, IsAny, Not } from "../helpers";
/**
* Check if an {@link Ary} (i.e., `Array` or `ReadonlyArray`) is a fixed length one (i.e., the
* `length` property is a number literal).
*
* Sig: `(a: Ary) => boolean`
*
* @example
* ```typescript
* type R1 = IsFixedLength<string[]>;
* // ^?: false
* type R2 = IsFixedLength<readonly number[]>;
* // ^?: false
* type R3 = IsFixedLength<["foo", "bar"]>;
* // ^?: true
* type R4 = IsFixedLength<readonly [42, 1337, ...string[]]>;
* // ^?: false
* ```
*/
export type IsFixedLength<A extends Ary> = If<
IsAny<A>,
boolean,
A extends { length: infer N } ? Not<Extends<Nat, N>> : never
>;
/**
* [Fn] Check if an {@link Ary} (i.e., `Array` or `ReadonlyArray`) is a fixed length one (i.e., the
* `length` property is a number literal).
*
* Sig: `(a: Ary) => boolean`
*/
export default interface IsFixedLengthFn extends Fn<[Ary], boolean> {
def: ([a]: Args<this>) => IsFixedLength<typeof a>;
}