Skip to content

Instantly share code, notes, and snippets.

@MNBuyskih
Created May 10, 2023 17:47
Show Gist options
  • Save MNBuyskih/03b0550c295254ca99e4f4e4e87c9704 to your computer and use it in GitHub Desktop.
Save MNBuyskih/03b0550c295254ca99e4f4e4e87c9704 to your computer and use it in GitHub Desktop.
Generate typings for selectors
import * as fs from "fs";
import * as path from "path";
function main(p: number): string {
function create(prefix: string, join = ", "): string {
return Array(p)
.fill("_")
.map((_, i) => `${prefix}${i + 1}`)
.join(join);
}
function create2(): string {
return Array(p)
.fill("_")
.map((_, i) => `ParametricSelector<S${i + 1}, P${i + 1}, R${i + 1}>`)
.join(", ");
}
function create3(): string {
return Array(p)
.fill("_")
.map((_, i) => `res${i + 1}: R${i + 1}`)
.join(", ");
}
const S = create("S");
const P = create("P");
const R = create("R");
const selectors = create2();
const combiner = create3();
return `
// ${p} selectors
export function createSelector<${S}, ${P}, ${R}, T>(
selectors: [${selectors}],
combiner: (${combiner}) => T
): OutputParametricSelector<${create("S", " & ")}, ${create("P", " & ")}, T, (${combiner}) => T>;`;
}
const fileData = [
`/* eslint-disable */
import {ParametricSelector, OutputParametricSelector} from "reselect";
declare module "reselect" {`,
];
for (let i = 14; i <= 21; i++) {
fileData.push(main(i));
}
fileData.push("}");
fs.writeFileSync(path.resolve(__dirname, "reselect.d.ts"), fileData.join("\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment