PrimitiveType
The primitive types including number
, string
, boolean
, undefined
, null
, bigint
, and symbol
Added in v0.0.1
Source
export type PrimitiveType =
| undefined
| null
| number
| string
| boolean
| bigint
| symbol
NumberString
A string composed of numbers.
Added in v0.0.1
Source
export type NumberString = `${number}`
ObjectLike
Non-function object.
Added in v0.0.1
Source
export type ObjectLike = object & { call?: never; [x: PropertyKey]: any }
ExtractUnion
Extract a union type from a tuple.
Added in v0.0.1
Source
export type ExtractUnion<T extends readonly string[]> = {
[K in keyof T]: T[K]
}[number]
KeyOrIndex
Extracts a number from a string in the form of [${number}] or ${number}. Otherwise, returns the original string.
Added in v0.0.1
Source
export type KeyOrIndex<T extends string> = T extends
| `[${infer D extends number}]`
| `${infer D extends number}`
? D
: T
Tail
Returns the last element of tuple type.
Added in v0.0.1
Source
export type Tail<T extends readonly any[]> = T extends readonly [
...any[],
infer L
]
? L
: never
Head
Returns the first element of tuple type.
Added in v0.0.1
Source
export type Head<T extends readonly any[]> = T extends readonly [
infer L,
...any[]
]
? L
: never
Edge
Retrieve the first or last element of tuple T
, determined by type D
.
Added in v0.0.1
Source
export type Edge<
T extends readonly any[],
D = 'left' | 'right'
> = D extends 'left' ? Head<T> : Tail<T>
EdgeReverse
Similar to Edge
, but the effects of 'left'
and 'right'
for D are reversed.
Added in v0.0.1
Source
export type EdgeReverse<
T extends readonly any[],
D = 'left' | 'right'
> = D extends 'right' ? Head<T> : Tail<T>
EmptyOrParameters
Returns the parameter types of the function; if the input is not a function, returns the never[]
type.
Added in v0.0.1
Source
export type EmptyOrParameters<T> = T extends (...args: any[]) => any
? Parameters<T>
: never[]
EmptyOrReturnType
Returns the return type of the function; if the input is not a function, returns the void
type.
Added in v0.0.1
Source
export type EmptyOrReturnType<T> = T extends (...args: any[]) => any
? ReturnType<T>
: void
WithFallback
Either returns the result of type T
or a fallback value R
if the result is null
or undefined
.
Added in v0.0.2
Source
export type WithFallback<T extends (...args: any[]) => any, R> =
ReturnType<T> extends undefined | null ? R : ReturnType<T>
LiteralStringWithFallback
This type provides a default string value R
when string type T
is too general (like string).
It ensures type safety while allowing flexibility in scenarios such as configuration objects or optional parameters.
Added in v0.0.2
Source
export type LiteralStringWithFallback<T extends string, R extends string> = T &
R extends never
? T
: R
MappedTypeByKeyOrIndex
Generates plain objects or arrays based on input string/numeric index T
, pointing to type V
, with optionality controlled by type O
.
Added in v0.0.2
Source
export type MappedTypeByKeyOrIndex<
T extends string,
V,
O extends boolean = false
> =
KeyOrIndex<T> extends string
? O extends false
? { [P in T]: V }
: { [P in T]?: V }
: O extends false
? unknown[] & { [P in KeyOrIndex<T>]: V }
: unknown[] & { [P in KeyOrIndex<T>]?: V }
DeepMappedTypeByKeyOrIndex
Recursively parses the field path T
and creates nested plain objects or arrays. It can interpret nested path strings like "data.[0].name"
, with the path's end field pointing to value V
. O
decides if the value is optional.
It is very useful for creating complex nested types based on string templates.
Added in v0.0.2
Source
export type DeepMappedTypeByKeyOrIndex<
T extends string,
V,
O extends boolean = false
> = T extends `[${infer Key extends number}][${infer Rest}`
? MappedTypeByKeyOrIndex<`${Key}`, DeepMappedTypeByKeyOrIndex<`[${Rest}`, V>>
: T extends `${infer Key}[${infer Rest}`
? MappedTypeByKeyOrIndex<
`${Key}`,
DeepMappedTypeByKeyOrIndex<`[${Rest}`, V>
>
: T extends `[${infer Key extends number}].${infer Rest}`
? MappedTypeByKeyOrIndex<`${Key}`, DeepMappedTypeByKeyOrIndex<Rest, V>>
: T extends `${infer Key}.${infer Rest}`
? MappedTypeByKeyOrIndex<`${Key}`, DeepMappedTypeByKeyOrIndex<Rest, V>>
: MappedTypeByKeyOrIndex<T, V, O>
DataUnit
DataUnit
type represents different units of digital data.
Added in v0.0.2
Source
export type DataUnit =
| 'bit'
| 'B'
| 'KB'
| 'MB'
| 'GB'
| 'TB'
| 'PB'
| 'EB'
| 'ZB'
| 'YB'