Here is a scenario to consider:
function example<T>(t: T): boolean {
return t === 1;
}
The message received states
This condition will always return 'false' since the types 'T' and 'number' have no overlap.
.
To resolve this, the function can be modified as follows:
function example<T extends any>(t: T): boolean {
return t === 1;
}
It was initially assumed that T
could be any
by default. Why then was it necessary to include extends any
? What type does TypeScript expect T
to be without extends any
?
Upon reviewing the actual function code:
function cleanObject<T>(
object: ObjectOf<T>,
): ObjectOf<T> {
const newObject = {};
for (const key of Object.keys(object)) {
if (object[key] !== null && typeof object[key] !== 'undefined' && object[key] !== false) {
newObject[key] = object[key];
}
}
return newObject;
}