Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property does not exists on type '{}' using Promises

I am accessing a property of an object returned from a resolved promise.

return new Promise((resolve) => {
    // Get result
    resolve(result)
}).then(r => console.log(r.id))

Typescript compiles the code and the code works but my IDE is complaining about r.id

[ts] Property 'id' does not exist on type '{}'.

What is the 'TypeScript' method of dealing with this? This question seems to have the same issue but I cannot understand the given solutions. This answer talks about using interfaces but im not sure how I would apply that to the then() function of a Promise

like image 882
myol Avatar asked Apr 16 '18 09:04

myol


People also ask

Does property 'then' exist on type ' { type: string?

TS2339: Property 'then' does not exist on type ' { type: string; payload: Promise< {}>; }' It sounds like I need to include Promise<...> somewhere as a type so typescript knows that then is in fact a property on the object that's returned by dispatcher () but I haven't been able to remove the error.

How to solve'property does not exist on type {}'error?

The "Property does not exist on type ' {}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

Does the id property exist on type'{}'in typescript?

[ts] Property 'id' does not exist on type ' {}'. What is the 'TypeScript' method of dealing with this? This question seems to have the same issue but I cannot understand the given solutions. This answer talks about using interfaces but im not sure how I would apply that to the then () function of a Promise Show activity on this post.

How to get the result type of a promise in typescript?

Typescript will not be able to tell the result type of the Promise by the usage of resolve, you need to specify the result type explicitly as a generic parameter to Promise: You can replace { id: string } with any type, as bonus typescript will check that resolve is called with the correct result type.


Video Answer


1 Answers

Typescript will not be able to tell the result type of the Promise by the usage of resolve, you need to specify the result type explicitly as a generic parameter to Promise:

new Promise<{ id: string }>((resolve) => {
    // Get result
    resolve(result)
}).then(r => console.log(r.id))

You can replace { id: string } with any type, as bonus typescript will check that resolve is called with the correct result type.

Edit I am assuming that instead of // Get result there is some more complicated code that requires use of the Promise constructor. If you already know the result, you can just use Promse.resolve(result) which will type the promise correctly as @BenjaminGruenbaum pointed out in the comments

like image 188
Titian Cernicova-Dragomir Avatar answered Oct 16 '22 19:10

Titian Cernicova-Dragomir