Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@GheorgheP
Created April 17, 2020 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GheorgheP/63b732ea02f4ed7116465fb5d124ea8c to your computer and use it in GitHub Desktop.
Save GheorgheP/63b732ea02f4ed7116465fb5d124ea8c to your computer and use it in GitHub Desktop.
type Null = undefined | null
type MVal<T> = T | Null
type Nullify<T> = {
[P in keyof T]: MVal<T[P]>;
};
type MParams<T extends (...args: any) => any> = T extends (...args: infer P) => any ? Nullify<P> : never;
declare function apply<F extends (...args: any) => any>(f:F, ...args: Parameters<F>): ReturnType<F>
declare function mApply<F extends (...args: any) => any>(f:F, ...args: MParams<F>): MVal<ReturnType<F>>
const inc = (a: number):number => a + 1
const sum = (a: number, b:number):number => a + b
const x1 = apply(inc, 3)
const x2 = apply(sum, 3, 4);
const x = mApply(sum, 3, null);
const y = mApply(inc, null);