I have a function that takes user input, but the argument type it receives is unknown
. I need to make sure that...
value
is an objectvalue
contains a key named"a"
function x(value: unknown){
if(value === null || typeof value !== 'object'){
throw new Error('Expected an object');
}
if(!('a' in value)){
throw new Error('Expected an object to contain property "a"');
}
}
Typescript is giving me an error saying "Object is possibly 'null'"...
https://i.sstatic.net/jt1zT.png
How can I specifically define unknown
as an object?