TypeScript playground link
I’m creating a route handler creator for Express for a project, and I’m trying to make it so that you can pass arbitrary assertions as initial arguments before passing the route handler callback. Something like this:
const myHandler = makeHandler(assertion1(), assertion2(), (data, req, res, next) => {
// data is an array of the results of the assertions
});
I can get some version of the way I want the types “working”:
// express namespace omitted for brevity; see playground link above.
type Assertion<T = unknown> = (req: express.Request) => T;
type ReturnTypes<A extends ReadonlyArray<Assertion>> = {
[K in keyof A]: ReturnType<A[K]>;
};
function assertion1<T extends object>(arg: T) {
return function inner(req: express.Request): T {
return arg;
}
}
function assertion2() {
return function inner(req: express.Request): string {
return "yes"
}
}
const a = assertion1({ something: "yes" })
const b = assertion2()
// This type works as expected: it is [{ something: string }, string]
type d = ReturnTypes<[typeof a, typeof b]>
However, when I try to get it working as a variadic version of the above for the arguments of makeHandler
, something doesn’t seem to work and the type of data
in the example above is unknown[]
:
function makeHandler<
Assertions extends Assertion[],
R = ReturnTypes<Assertions>,
>(...assertionsAndCb: [...Assertions, HandlerCb<R>]) {
return async function handlerImpl(
req: express.Request,
res: express.Response,
next: express.NextFunction
) {
const assertions = assertionsAndCb.slice(0, -1) as Assertions;
const [cb] = assertionsAndCb.slice(-1) as [HandlerCb<R>];
const data = [];
for (const assertion of assertions) {
try {
data.push(assertion(req));
} catch (e) {
next(e);
return;
}
}
try {
await cb(data as R, req, res, next);
} catch (e) {
next(e);
}
};
}
// `data` here doesn't seem to work, though. For some reason it's of type unknown[], rather than
// the same as type `d` above.
makeHandler(assertion1({ hey: "what"}), assertion2(), (data, req) => {
return { response: {} }
})
I’ve read a bit about how this might work for something like zip
(and heavily based my function on this gist), but I’m struggling to get the actual types to correctly pass. Is there something that I’m missing here – some generic that I’m not correctly allowing to be inferred, for example?