Hi all! I would appreciate some clearance on my following concern. I'm pretty new and currently studying generics and trying to figure out why in this snippet, cache recognizes the 'save' property:
type CacheHost = {
save: (a: any) => void;
}
function addObjectToCache<Type, Cache extends CacheHost>(obj: Type, cache: Cache): Cache {
cache.save(obj);
return cache;
}
But it does not if I write it like this, when it clearly has 'save' as a property anyway??:
type CacheHost = {
save: (a: any) => void;
}
function addObjectToCache<Type, CacheHost>(obj: Type, cache: CacheHost): CacheHost {
cache.save(obj);
return cache;
}
Property 'save' does not exist on type 'CacheHost'.(2339)
What is the difference between declaring one type and then extending to some other type instead of assigning that one type directly?