Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 1.47 KB

README.md

File metadata and controls

54 lines (42 loc) · 1.47 KB

ts-typelevel-fp

Functional type-level programming with higher kinded types

This project is about type-level programming in typescript. It uses module augmentation to create higher kinded types, similar to fp-ts.

export type IsString<T> = T extends string ? true : false;

export const IsStringF: unique symbol = Symbol();
export type IsStringF = typeof IsStringF;

declare module '../hkt' {
    interface Kind1Table<T> {
        [IsStringF]: IsString<T>;
    }
}

With Apply (and the shorthand $) a higher kinded type can be applied to a type:

type X = Apply<IsStringF, 'hello'>;
// or
type X = $<IsStringF, 'hello'>;

Partial application:

type F = $<AppendStringsF, 'a'>
type S = $<F, 'b'>  // 'ab'

Partial application of the second argument:

type F = $2<AppendStringsF, 'a'>
type S = $<F, 'b'> // 'ba'

Example using Filter:

type Input = ['hello', 123, true, 'world', 234];
type Output = Filter<IsStringF, Input> // ['hello', 'world']

Example using FMap and branching:

type Func = $<$<$<IfF, IsStringF>, $2<AppendStringsF, '()'>>, IdF>;
type Input = ['hello', 123, 'world', null];
type Output = FMap<Func, Input>; // ["hello()", 123, "world()", null]

This project uses combinators like CurryF, UncurryF, FlipF, ComposeF, SCombinatorF, IdF, ConstF and IfF to allow point-free programming on the type-level.

For more examples, see tests1.ts