1.0.1 • Published 2 years ago
unknown-object v1.0.1
What type do you use for an object that may contain anything, for example a JSON response from an endpoint?
The problem with any
is that it allows unsafe access:
var a: any;
a.foo.bar; // What if .foo is undefined?
The problem with unknown
and object
is they don't allow any access
Enter UnknownObject
. It allows getting arbitrary properties, but only safely:
var u: UnknownObject;
u.foo?.bar; // This is fine
u.foo.bar; // However, .foo might not be there, so this is a type error!