'how to define function with generic in typescript
I am defined a function api_post_con like this in typescript:
export const RequestHandler = {
api_post_con:(url: string,data: any) => {
}
}
now I want the api_post_con function with generic like this function:
export function api_post<T>(url: string,data: any): Promise<T> {
return fetch(url,{
method: 'POST',
headers: {
'Content-type': 'application/json',
'x-access-token': 'xxxxx'
},
body: JSON.stringify(data),
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json() as Promise<T>
})
}
how to change the api_post_con function just like api_post function that return generic type? I have tried like this but did not work:
export const RequestHandler = {
api_post_con<T>:(url: string,data: any):Promise<T> => {
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
