According to TypeScript handbook, there is a type inferring in function type declaration. But when I introduce generic type into function declaration, the type inferring somehow not working. I have to explicitly write the type. Why the type inferring is not working when there is generic type?
const fn1: (x: number, y: number) => number = (x, y) => {
// working
return x + y;
}
const fn2: <T>(x: number, y: number) => Promise<T> = <T>(x, y) => {
// not working, although ws hints these parameters' type is number
...use type T somewhere...
...x + y;
...
}
Edit: Thank you for the comment. But my question is different.
const fn1: (x: number, y: number) => number = (param1, param2) => {
// typescript will infer the type of param1 and param2 is number
return param1 + param2;
}
const fn2: <T>(x: number, y: number) => number = <T>(param3, param4) => {
// I think typescript will infer the type of param3 and param4 is number
// but with strict flag true (noImplicitAny), there is a compile error says param3 and param4 is type any
// why adding generic type to function will cause type inferring not working?
return param3 + param4;
}
Edit 2: Here is the actual code from the project
type Request = <T>(method: Method, url: string, params?: Record<string, unknown>) => Promise<T>;
const request: Request = async <T>(
// because I have declare the parameters type in type Request
// why I have to declare the type again here for the parameters?
// if I omit types here, typescript thinks these params are 'type any'
method: Method, url: string, params: Record<string, unknown> = {},
) => {
const fetchConfig: RequestInit = {
method,
credentials: 'same-origin',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
};
let targetUrl = url;
switch (method) {
case Method.Get:
case Method.Delete:
targetUrl += qs.stringify(params, { addQueryPrefix: true, arrayFormat: 'comma' });
break;
case Method.Post:
fetchConfig.body = JSON.stringify(params);
break;
default:
}
const resp = await fetch(targetUrl, fetchConfig);
// I need to use generic type T here
const respJson = await resp.json() as T;
if (resp.status >= 200 && resp.status < 300) {
return respJson;
}
if (resp.status === 401) {
globalThis.history.replaceState(undefined, '', RouteURL.adminLogin);
}
const newError = new Error('Request failed');
Object.assign(newError, { json: respJson });
throw newError;
};