Skip to content

PrimitiveType

The primitive types including number, string, boolean, undefined, null, bigint, and symbol

Added in v0.0.1

Source

typescript
export type PrimitiveType =
  | undefined
  | null
  | number
  | string
  | boolean
  | bigint
  | symbol

NumberString

A string composed of numbers.

Added in v0.0.1

Source

typescript
export type NumberString = `${number}`

ObjectLike

Non-function object.

Added in v0.0.1

Source

typescript
export type ObjectLike = object & { call?: never; [x: PropertyKey]: any }

ExtractUnion

Extract a union type from a tuple.

Added in v0.0.1

Source

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
export type EmptyOrReturnType<T> = T extends (...args: any[]) => any
  ? ReturnType<T>
  : void

WithFallback

T の結果を返すか、結果が null または undefined の場合にデフォルト値の型 R を返します。

Added in v0.0.2

Source

typescript
export type WithFallback<T extends (...args: any[]) => any, R> =
  ReturnType<T> extends undefined | null ? R : ReturnType<T>

LiteralStringWithFallback

文字列型 T が広範囲(例えば string)の場合、この型はデフォルトの文字列値 R を提供します。

これは、構成オブジェクトやオプションのパラメータなどのシナリオで型の安全性を確保しながら、柔軟性も確保します。

Added in v0.0.2

Source

typescript
export type LiteralStringWithFallback<T extends string, R extends string> = T &
  R extends never
  ? T
  : R

MappedTypeByKeyOrIndex

入力文字列/数値インデックス T に基づいて、平たく単純なオブジェクトまたは配列の型を生成し、そのインデックスが型 V を指し、型 O が値のオプション性を制御します。

Added in v0.0.2

Source

typescript
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

フィールドパス T を再帰的に解析し、ネストされた平たく単純なオブジェクトまたは配列を作成します。"data.[0].name" のようなネストされたパス文字列を解釈し、パスの末端フィールドが値 V を指します。O が値がオプションかどうかを決定します。

これは、文字列テンプレートに基づいて複雑なネストされた型を作成するのに非常に役立ちます。

Added in v0.0.2

Source

typescript
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 型は、デジタルデータの異なる単位を表します。

Added in v0.0.2

Source

typescript
export type DataUnit =
  | 'bit'
  | 'B'
  | 'KB'
  | 'MB'
  | 'GB'
  | 'TB'
  | 'PB'
  | 'EB'
  | 'ZB'
  | 'YB'