I'm trying to type guard an unknown type
const foo = (obj: unknown) => {
if (typeof obj === 'object' && obj) {
if ('foo' in obj && typeof obj.foo === 'string') {
return obj.foo;
}
}
};
Property 'foo' does not exist on type 'object'.
I also tried with is expression does not work:
const foo = (obj: unknown): obj is { foo: 'string' } => {
if (typeof obj === 'object' && obj) {
if ('foo' in obj && typeof obj.foo === 'string') {
return obj;
}
}
throw new Error();
};