hkt-core simplifies the use of Higher-Kinded Types in TypeScript, ensuring type safety while providing elegant implementations. Whether for type-level functions like in HOTScript or type classes like in fp-ts, hkt-core offers developers a robust and streamlined experience with the power of HKTs.
hkt-core
š hkt-core is a minimal implementation of Higher-Kinded Types (HKT) tailored for TypeScript. This library guarantees type safety while providing a streamlined experience for both classical HKT use cases and type-level function programming.
Overview
Higher-Kinded Types are a sophisticated concept commonly utilized in renowned TypeScript libraries like fp-ts, Effect, TypeBox, and HOTScript. While these libraries embrace HKTs, their diverse implementations create challenges in sharing functionalities across different libraries. hkt-core addresses this issue by delivering a standardized and type-safe approach to HKTs, which can effortlessly integrate with existing TypeScript codebases.
Core Features
- Classical HKT Operations: Designed for familiar HKT use cases like monad implementations.
- Type-Level Functions: Supports functional type-level programming, allowing users to define complex type operations with elegance and clarity.
- Zero-Cost Abstractions: Optimized for performance, ensuring that type computations do not compromise TypeScript's compilation speed.
- Minimalistic Design: Focused solely on core HKT functionalities, ensuring easy adoption and integration without unnecessary bloat.
Usage Examples
Below are snippets showcasing how to use hkt-core in diverse scenarios:
/* Use as type-level functions (e.g., HOTScript style) */
type ConcatNames<Names extends string[]> = Pipe<
Names, // [1]
Filter<Flow<StringLength, NotExtend<1 | 2>>>, // Filter out short names
Map<CapitalizeString>, // Capitalize each name
JoinBy<", "> // Join names with a comma
>;
type _ = ConcatNames<["alice", "bob", "i"]>; // => "Alice, Bob"
/* Use as classical HKTs (e.g., fp-ts style) */
interface MonadTypeClass<F extends HKT> {
of: <T>(a: T) => Kind<F, T>; // Lift a value into the monad
flatMap: <T, U>(fa: Kind<F, T>, f: (a: T) => Kind<F, U>) => Kind<F, U>;
}
// Create a `flatten` function for a monad from a monad type class
const createFlatten =
<F extends HKT>(monad: MonadTypeClass<F>) =>
<T>(ffa: Kind<F, Kind<F, T>>): Kind<F, T> =>
monad.flatMap(ffa, (x) => x);
[1]: This is just an example to demonstrate type-level functions. Some types used here (e.g., Filter, Map) are not built into hkt-core. See the following sections for more details.
No comments yet.
Sign in to be the first to comment.