tl;dr why does
const func = (a: unknown) => { if (a && typeof a === 'object' && 'b' in a) { a.b; } }; Give the following error message
Property 'b' does not exist on type 'object'. ?
Edit: After looking into this more closely I have an even more minimal example. So let me rephrase my question:
How to probably narrow object type in TypeScript?
tl;dr why does
const func = (a: object) => { if ('b' in a) { a.b; } give the following error message:
Property 'b' does not exist on type 'object'. ?
anyandunknown. If this werea: anythis wouldn't even be a question. However,unknownis a good default, instead ofanyRecord<string, unknown>which would describe an object with any keys with an unknown type on the value of this key. If you do this typescript can narrow down the correct property. Have a look at this playgroundunknowntoRecord<string, unknown>?