Skip to content

Instantly share code, notes, and snippets.

@ENvironmentSet
Last active August 28, 2023 04:47
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Encoding HKTs in typescript without declaration merging
export interface HKT {
param: unknown;
result: unknown;
}
interface NotHKT extends HKT {
result: this['param'] extends true ? false : true;
}
interface FstHKT extends HKT {
result: this['param'] extends [infer A, infer _] ? A : never;
}
interface ArrayHKT extends HKT {
result: Array<this['param']>;
}
export type Apply<f extends HKT, x>
= (f & { param: x })['result'];
type Test1 = Apply<NotHKT, true> // be deducedd to `false`
type Test2 = Apply<FstHKT, [string, boolean]> // be deduced to `string`
type Test3 = Apply<ArrayHKT, number> // be deduced to `number[]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment