To delve deeper into type narrowing, you can visit the following link: https://www.typescriptlang.org/docs/handbook/2/narrowing.html
Regarding your issue, it seems like implementing a function overload might be necessary. However, based on the limited information provided in your question, I recommend considering a different design approach. Maybe avoiding a generic use-case as this particular scenario doesn't require such generality. The API call methods have remained consistent and are unlikely to change soon. Instead of seeking a generic solution, focus on narrowing down to a fixed type.
Your query appears somewhat ambiguous. It's crucial to be wary of falling into the XY Problem trap, where you present a solution rather than discussing the actual problem at hand.
A potential solution could involve utilizing a typeguard. To infer a type from a value (excluding boolean), employing a typeguard is typically the route to take.
If your objective is to deduce the parameter type (rather than the value), incorporating a function overload would be essential.
I've outlined an example of a Typeguard system that may be tailored to suit your specific situation. While the syntax differs from a standard typeguard, the underlying principle remains similar to type-narrowing techniques.
type Methods = "GET" | "POST";
function func<Params>(method: Methods, params?: Params) {
method;
if (method === "GET") {
method;
}
method;
if (method === "GET") {
method;
return;
}
method;
}
Here's an authentic example of a "typeguard".
type StringOrNumber = string | number;
function narrowStringOrNumber(value: StringOrNumber): value is string {
if (typeof value === "string") {
return true;
}
return false;
}
const a: StringOrNumber = "Hello";
a; // StringOrNumber
if (narrowStringOrNumber(a)) {
a; // string
}