Attempting to articulate our situation the best I can, given the lack of a specific title.
We have developed a package that facilitates communication between our various services and cloud functions. This package includes functions to call each service (e.g., an auth
function for the authentication service). With an increasing number of endpoints for each service, it has become challenging to maintain due to the complexity of defining parameter types for these requests.
My goal is to create another type that outlines the parameter types for each endpoint of every service. For example, if we have an endpoint named getEmail
which requires an id
parameter, the type params would be structured like this:
type Params = {
getEmail: {
id: number;
}
}
A simplified snippet of the code:
type FunctionName = 'foo' | 'bar' | 'baz';
type FunctionParams = {
foo: {
myVar: number;
};
bar: {
myVar: string;
};
baz: Record<string, number>;
};
declare const sendRequest: (...args: any[]) => any;
const callFunction = (
fn: FunctionName,
params: FunctionParams[typeof fn],
// ^^^^^^^^^ What should I put here?
) => {
sendRequest(fn, params);
};
callFunction('foo', {
myVar: 'this should fail',
// This is allowed, as the type of the second parameter is:
// { myVar: number; } | { myVar: string; } | Record<string, number>) => void
// I want it to be { myVar: number; }
});