Recently, I made the switch from JavaScript to TypeScript and now I am in the process of converting a lot of my code.
I used to frequently use this snippet in JavaScript (which has been converted to TypeScript):
function isObject (payload: any): payload is object {
const type: string = Object.prototype.toString.call(payload).slice(8, -1)
return type === 'Object'
}
In JavaScript, I would then proceed to do things like:
if (isObject(payload) && payload.id) return payload.id
However, when switching to TypeScript, I encountered an error stating that id
does not exist on object
.
Instead of returning payload is object
in my isObject
function, I believe it might be better to return an object with any properties having any values.
What would be the best approach to achieve this?