error TS2571: Object is of type ‘unknown’.
Broken Code ❌
If you use third-party libraries then it can happen that TypeScript cannot infer all return types. In such a case a return type can be
unknown which makes it impossible to access its properties from TypeScript. In the following example such case is constructed by assigning
unknown to an exemplary constant:
Fixed Code ✔️
To solve the problem of accessing properties from an unknown object, we have to define the type of the object which we want to access:
You may also get the error “Object is of type ‘unknown’” when catching errors. In this case you have to type the error in your catch clause.