I am developing a web application that uses cookies. To read a cookie I would like to write a typescript function of this type:
let cookie1: number = getCookie<number>("fake_int_name");
let cookie2: boolean = getCookie<boolean>("fake_bool_name");
let cookie3: string = getCookie<string>("fake_string_name");
function getCookie<T>(name: string): T {
...
let cookie_value: string = "fake_cookie_value";
if(T is number)
return parseInt(cookie_value);
if(T is boolean)
return cookie_value == "true";
return cookie_value;
}
Is it possible to create a getCookie function able of behaving differently depending on the type T?