Consider this example:
function foo<T>(t: T): boolean {
return t === 1;
}
I get This condition will always return 'false' since the types 'T' and 'number' have no overlap..
The error goes away if I change it to:
function foo<T extends any>(t: T): boolean {
return t === 1;
}
I thought T could be any by default, so why did I need extends any? Without extends any, what type does TS expect T to be?
Edit, actual function:
function cleanObj<T>(
obj: ObjectOf<T>,
): ObjectOf<T> {
const newObj = {};
for (const k of Object.keys(obj)) {
if (obj[k] !== null && typeof obj[k] !== 'undefined' && obj[k] !== false) {
newObj[k] = obj[k];
}
}
return newObj;
}
anyby default, it's behaviour is more likeunknownwhich for your case, would also fail because you can't compare to a type that you're not aware of. As @MoxxiManagarm said, if you don't need the generics, just get rid of them.Twould be known, isn't that the whole point of generics? Even if it's unknown, the error "always return 'false'" is still wrong, sinceunknowndoesn't mean it can't be a number<T extends unknown>works fine, the same asextends anyunknown, you just can't assign anything to it.