Skip to content

Ts.Kind

Ts / Kind

Higher-kinded type utilities for TypeScript.

Provides type-level functions and utilities for simulating higher-kinded types in TypeScript, enabling more advanced type-level programming patterns.

Import

typescript
import { Ts } from '@wollybeard/kit'

// Access via namespace
Ts.Kind.someFunction()
typescript
import * as Ts from '@wollybeard/kit/ts'

// Access via namespace
Ts.Kind.someFunction()

Constants

[C] PrivateKindReturn

typescript
typeof PrivateKindReturn

Private symbol for storing kind return type.

[C] PrivateKindParameters

typescript
typeof PrivateKindParameters

Private symbol for storing kind parameters.

Types

[T] Apply

typescript
type Apply<$Kind, $Args> =
  // @ts-expect-error - Intentional type manipulation for kind simulation
  ($Kind & { parameters: $Args })['return']

Higher-kinded type utilities for TypeScript.

Provides type-level functions and utilities for simulating higher-kinded types in TypeScript, enabling more advanced type-level programming patterns.

[I] Kind

typescript
interface Kind<$Params = unknown, $Return = unknown> {
  readonly parameters: $Params
  readonly return: $Return
}

Define a kind (higher-kinded type) function interface.

Provides a standard structure for defining type-level functions that can be applied using the Apply utility.

$Params

  • The parameter types this kind accepts

$Return

  • The return type this kind produces

Examples:

typescript
interface BoxOf extends Kind<[unknown], 
Box
<any>> {
return
:
Box
<this['parameters'][0]>
}

[T] Parameters

typescript
type Parameters<$Kind> = $Kind extends Kind<infer P, any> ? P : never

Extract the parameter types from a kind.

$Kind

  • The kind to extract parameters from

[T] Return

typescript
type Return<$Kind> = $Kind extends Kind<any, infer R> ? R : never

Extract the return type from a kind.

$Kind

  • The kind to extract return type from

[I] Identity

typescript
interface Identity extends Kind {
  // @ts-expect-error
  return: this['parameters'][0]
}

Create a type-level identity function.

Returns the input type unchanged. Useful as a default or placeholder in kind compositions.

Examples:

typescript
type 
Same
= Kind.
Apply
<Kind.
Identity
, [string]> // string

[I] Const

typescript
interface Const<$Const> extends Kind {
  return: $Const
}

Create a type-level constant function.

Always returns the same type regardless of input.

$Const

  • The constant type to always return

Examples:

typescript
type 
AlwaysString
= Kind.
Apply
<Kind.
Const
<string>, [number]> // string

[T] PrivateKindReturn

typescript
type PrivateKindReturn = typeof PrivateKindReturn

[T] PrivateKindParameters

typescript
type PrivateKindParameters = typeof PrivateKindParameters

[I] Private

typescript
interface Private {
  [PrivateKindReturn]: unknown
  [PrivateKindParameters]: unknown
}

Private kind interface using symbols instead of string keys.

This provides a more secure way to define higher-kinded types as the symbols cannot be accessed outside the module.

Examples:

typescript
interface BoxKind extends PrivateKind {
  // @ts-expect-error
  [PRIVATE_KIND_RETURN]: 
Box
<this[
PRIVATE_KIND_PARAMETERS
][0]>
[PRIVATE_KIND_PARAMETERS]: unknown }

[T] PrivateApply

typescript
type PrivateApply<$Kind extends Private, $Args> =
  ($Kind & { [PrivateKindParameters]: $Args })[PrivateKindReturn]

Apply arguments to a private kind.

$Kind

  • The private kind to apply

$Args

  • The arguments to apply

Examples:

typescript
type 
BoxOfString
=
PrivateKindApply
<
BoxKind
, [string]> // Box<string>

[T] MaybePrivateApplyOr

typescript
type MaybePrivateApplyOr<$MaybeKind, $Args, $Or> = $MaybeKind extends Private
  ? PrivateApply<$MaybeKind, $Args>
  : $Or

[T] IsPrivateKind

typescript
type IsPrivateKind<T> = T extends Private ? true : false

Check if a type is a private kind.

T

  • The type to check

Examples:

typescript
type 
Test1
=
IsPrivateKind
<
BoxKind
> // true
type
Test2
=
IsPrivateKind
<string> // false