I want to specify type in that way, that its generic parameter should be an object that can be parsed from json. So no functions, no dates, no other kinds of values that can't be represented in json. How can I do that?
function get<T>(url: string): Promise<T> {
return fetch(url).then(resp => resp.json())
}
interface Type1 {
x: string;
}
interface Type2 {
x: string;
getX(): string;
}
interface Type3 {
x: Date;
}
interface Type4 {
x: {
a: number[];
b: { z: (string | null)[] };
};
}
async function main() {
var a = await get<Type1>("/")
var b = await get<Type2>("/") // Want a error
var c = await get<Type3>("/") // Want a error
var d = await get<Type4>("/")
}