HKT
Provides type-level helpers for generic code over container-like types.
TypeScript cannot directly abstract over shapes such as Option<A>, ReadonlyArray<A>, or Effect<A, E, R>. This module represents those shapes with TypeLambda and applies concrete type arguments with Kind. It is mostly useful when defining generic helpers or type classes that should work across several data types.
Models
Signature
interface TypeClass<F extends TypeLambda> {
readonly [URI]?: F;
}TypeLambda interface
Base interface for defining Higher-Kinded Type parameters.
When to use
Use to encode a type constructor for higher-kinded generic programming.
Details
A TypeLambda encodes the "shape" of a type constructor, specifying how many type parameters it takes and their variance (contravariant, covariant, or invariant). The four parameters are In for contravariant input, Out2 for covariant output often used for errors, Out1 for covariant output often used for context or environment, and Target for the invariant main type.
Signature
interface TypeLambda {
readonly In: unknown;
readonly Out1: unknown;
readonly Out2: unknown;
readonly Target: unknown;
}Symbols
Defines the unique symbol used to associate TypeClass implementations with their TypeLambda.
When to use
Use when you need to define a custom type class that exposes the TypeLambda it operates on.
Details
This symbol links a type class shape with its compile-time type lambda. It is intended for type-class definitions and has no runtime behavior.
Signature
declare const URI: unique symbol;Utility Types
Applies type parameters to a TypeLambda to get the concrete type.
When to use
Use to apply a TypeLambda to type parameters and obtain its concrete type.
Details
This type-level function takes a TypeLambda and four type parameters, then "applies" them to get the actual type. It handles variance correctly, ensuring contravariant parameters are used as inputs and covariant parameters as outputs. This is the core mechanism that allows HKT to transform abstract type constructors into concrete types by applying arguments.
Signature
type Kind<F extends TypeLambda, In, Out2, Out1, Target> = F extends {
readonly type: unknown;
}
? F &
{
readonly In: In;
readonly Out1: Out1;
readonly Out2: Out2;
readonly Target: Target;
}["type"]
: {
readonly F: F;
readonly In: Types.Contravariant<In>;
readonly Out1: Types.Covariant<Out1>;
readonly Out2: Types.Covariant<Out2>;
readonly Target: Types.Invariant<Target>;
};
Base interface for type classes that work with Higher-Kinded Types.
When to use
Use to define type class interfaces parameterized by a
TypeLambda.Details
A
TypeClassdefines operations that can be performed on any type constructor that matches the givenTypeLambda. This enables writing generic code that works across different container types like Array, Option, Effect, etc.