Post

Right, I know how to work around it. But I am asking: Shouldn’t this be accepted by the type checker (because `null?.a` is perfectly valid)

That's just how the object type works in typescript. You're asserting it's an object and then trying to use it like `any` but it's not equivalent.

0 reposts0 likes

Yes. I know how typescript currently works. I'm claiming that TS should change it's behavior here (hence why I said "this should not be a type error, right?"). I thought you were saying that what I was doing here is wrong at the type theory level, which I don't think is the case.

I disagree and think the type error is correct. How do you propose to change the behaviour? What type should typeof === 'object' give you? It appears that you want it to be `any`

Your code is not checking if the property x exists on o, it's checking the value of the type is a string. The property could exist but be undefined. The correct way to check if a property exists is via 'x' in o.