Switching from C# to Typescript, I am trying to figure out how to return the default value of a generic parameter. In C#, we can achieve this with a simple function like so:
private T TwiceAsMuch<T>(T number)
{
if (number is int n)
return (T)(object)(n * 2);
else if (number is float f)
return (T)(object)(f * 2);
else
return default;
}
But how do I accomplish the equivalent of return default
in Typescript?
Context
I am working on a wrapper function that makes use of axios.post
and should return the response in the specified type:
private static async postInternal<T>(route: string, data: unknown): Promise<T> {
await Vue.$axios.get('/sanctum/csrf-cookie')
const res = await Vue.$axios.post<T>(`${route}`, data)
.then(res => res.data)
.catch(err => {
openErr(err)
return ???
})
}
What should replace ???
in this code snippet?