Skip to content

Instantly share code, notes, and snippets.

@frankpf
Last active April 30, 2019 05:58
Show Gist options
  • Save frankpf/439de122db18407786fe7faab4cf7319 to your computer and use it in GitHub Desktop.
Save frankpf/439de122db18407786fe7faab4cf7319 to your computer and use it in GitHub Desktop.
class Maybe<A> {
[Generic.repr]: Generic<MaybeRepr, A>
}
interface MaybeRepr extends Repr {
type: Maybe<this[1]>
}
type test1 = Of<MaybeRepr, string> // Maybe<string>
class Result<T, E> {
[Generic.repr]: Generic<ResultRepr, T, E>
}
interface ResultRepr extends Repr {
type: Result<this[1], this[2]>
}
type test2 = Of<ResultRepr, number, string> // Result<number, string>
/* Original repository: https://github.com/strax/tshkt */
export interface Repr {
1: unknown
2: unknown
3: unknown
4: unknown
type: unknown
}
export type Generic<
TRepr,
A1,
A2 = unknown,
A3 = unknown,
A4 = unknown
> = TRepr & TypeArgument<A1, A2, A3, A4>
export namespace Generic {
export declare const repr: unique symbol
}
export interface TypeArgument<
T1 = unknown,
T2 = unknown,
T3 = unknown,
T4 = unknown
> {
1: T1
2: T2
3: T3
4: T4
}
export interface HasGeneric<T, A1, A2 = unknown, A3 = unknown, A4 = unknown> {
[Generic.repr]: Generic<T, A1, A2, A3, A4>
}
export type Of<T, A1, A2 = unknown, A3 = unknown, A4 = unknown> = [T] extends [Repr]
? Generic<T, A1, A2, A3, A4>['type']
: HasGeneric<T, A1, A2, A3, A4>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment