Here's a TypeScript type that achieves what you want:
type AnyObject = {
[key: string]: AnyObject | any | undefined
}
This recursive type definition ensures:
Example usage:
const obj: AnyObject = {}
obj.a?.b?.c // ✓ OK
obj.a.b.c // ✗ Error: Object is possibly undefined
obj.fn?.() // ✓ OK
obj.arr?.[0]?.prop // ✓ OK