Is there a way to create a type guard for an object directly in TypeScript?
I've written a function to check any input:
export function isObject(input: any) :input is Record<string,any> {
return (input !== null) && (typeof input === 'object');
}
However, when I try to use it, the compiler doesn't recognize my checked variable as the correct type:
const constants = {};
const haveConstantsObj = isObject(constants);
// error: Object is possibly 'null'.ts(2531)
if (haveConstantsObj && !constants.householdType) {
console.log('hey');
}
What mistake am I making here?