79305278

Date: 2024-12-24 09:59:24
Score: 0.5
Natty:
Report link

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
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: yamumsahobo