Skip to content

Instantly share code, notes, and snippets.

@lierdakil
Created April 20, 2018 07:28
Show Gist options
  • Select an option

  • Save lierdakil/f4d1888dd1d708df0378a53740965812 to your computer and use it in GitHub Desktop.

Select an option

Save lierdakil/f4d1888dd1d708df0378a53740965812 to your computer and use it in GitHub Desktop.
Kinda-sorta variadic functions in TypeScript?
type SubsString<T extends (...args: any[]) => any> =
T extends (x1: infer U1,) => any ? (x1: U1,) => string:
T extends (x1: infer U1,x2: infer U2,) => any ? (x1: U1,x2: U2,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,) => any ? (x1: U1,x2: U2,x3: U3,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,x4: infer U4,) => any ? (x1: U1,x2: U2,x3: U3,x4: U4,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,x4: infer U4,x5: infer U5,) => any ? (x1: U1,x2: U2,x3: U3,x4: U4,x5: U5,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,x4: infer U4,x5: infer U5,x6: infer U6,) => any ? (x1: U1,x2: U2,x3: U3,x4: U4,x5: U5,x6: U6,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,x4: infer U4,x5: infer U5,x6: infer U6,x7: infer U7,) => any ? (x1: U1,x2: U2,x3: U3,x4: U4,x5: U5,x6: U6,x7: U7,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,x4: infer U4,x5: infer U5,x6: infer U6,x7: infer U7,x8: infer U8,) => any ? (x1: U1,x2: U2,x3: U3,x4: U4,x5: U5,x6: U6,x7: U7,x8: U8,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,x4: infer U4,x5: infer U5,x6: infer U6,x7: infer U7,x8: infer U8,x9: infer U9,) => any ? (x1: U1,x2: U2,x3: U3,x4: U4,x5: U5,x6: U6,x7: U7,x8: U8,x9: U9,) => string:
T extends (x1: infer U1,x2: infer U2,x3: infer U3,x4: infer U4,x5: infer U5,x6: infer U6,x7: infer U7,x8: infer U8,x9: infer U9,x10: infer U10,) => any ? (x1: U1,x2: U2,x3: U3,x4: U4,x5: U5,x6: U6,x7: U7,x8: U8,x9: U9,x10: U10,) => string:
(...args: any[]) => string
declare function memoizeWith<T extends (...args: any[]) => any>(keyFn: SubsString<T>, fn: T): T
const f = memoizeWith(() => "", (a: number, b: number, c: string) => a + b + c) // ok
const g = memoizeWith((x1: number) => "", (a: number, b: number, c: string) => a + b + c) // ok
const h = memoizeWith((x1: string) => "", (a: number, b: number, c: string) => a + b + c) // type error
const p = memoizeWith((x1: number, x2: number, x3: string) => "", (a: number, b: number, c: string) => a + b + c) // ok
const r = memoizeWith((x1: number, x2: number, x3: string, x4?: boolean) => "", (a: number, b: number, c: string) => a + b + c) // ok
const s = memoizeWith((x1: number, x2: number, x3: string, x4: boolean) => "", (a: number, b: number, c: string) => a + b + c) // type error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment