Skip to content

Instantly share code, notes, and snippets.

@Beraliv
Last active January 22, 2018 20:49
Show Gist options
  • Save Beraliv/9c0c6166d9400af34a2ee4eacf4ec976 to your computer and use it in GitHub Desktop.
Save Beraliv/9c0c6166d9400af34a2ee4eacf4ec976 to your computer and use it in GitHub Desktop.
Generator of Curried function interface for index.d.ts
const g = n => {
const array = Array.from(Array(n), (_, i) => i + 1);
const indices = Array.from(Array(n + 1), (_, i) => i);
const returnF = k => {
const types = array.slice(k).map(t => `T${t}`);
return types.length === 0
? 'R'
: `CurriedFunction${n - k}<${types.join(', ')}, R>`;
}
const params = k => {
const types = array.slice(0, k).map(t => `t${t}: T${t}`);
return `(${types.join(', ')})`;
}
return `
interface ${returnF(0)} {
${indices.map(i => {
return `${params(i)}: ${returnF(i)};`;
}).join('\n ')}
}
`
}
for (let i = 1; i <= 20; i++) {
console.log(g(i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment