Skip to content

Instantly share code, notes, and snippets.

@Beraliv
Created January 22, 2018 21:28
Show Gist options
  • Save Beraliv/39c69262e635388aea9d241d1d0ae916 to your computer and use it in GitHub Desktop.
Save Beraliv/39c69262e635388aea9d241d1d0ae916 to your computer and use it in GitHub Desktop.
Generator of Curried function types for index.js.flow
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}`);
return types.length === 0
? '()'
: `(...params: [${types.join(', ')}])`;
}
return `
export type ${returnF(0)} =
${indices.map(i => {
return `(${params(i)} => ${returnF(i)})`;
}).join(' &\n ')};`
}
for (let i = 1; i <= 10; i++) {
console.log(g(i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment