TS2571

error TS2571: Object is of type ‘unknown’.

Bad 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:

1
2
const x: unknown = 1337;
console.log(x.toString());

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:

1
2
const x: number = 1337;
console.log(x.toString());