Property does not exist on type void in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 1, 2022

banner

Photo from Unsplash

Property does not exist on type void in TypeScript #

The "Property does not exist on type void" error occurs when we try to access a property on the return value of a function that doesn't return anything. To solve the error, make sure to return the correct value from all of the function's code paths.

Here is an example of how the error occurs:

index.ts
const getPromise = () => { Promise.resolve(42); }; // ⛔️ Error: Property 'then' does not // exist on type 'void'.ts(2339) getPromise().then((value) => { console.log(value); });

The getPromise function does not return a value so TypeScript sets its return type to be void.

index.ts
// 👇️ const getPromise: () => void const getPromise = () => { Promise.resolve(42); };

The function implicitly returns undefined, so we can't access a property on an undefined value.

To solve the error, make sure to correct the return value of your function before accessing a specific property.

index.ts
// 👇️ const getPromise: () => Promise<number> const getPromise = () => { return Promise.resolve(42); }; // ✅ Works getPromise().then((value) => { console.log(value); // 👉️ 42 });

We used the return keyword to explicitly return a value from the function. Notice that TypeScript types the function's return type as Promise<number> and not void.

Now we can access the then() method on the function because it returns a valid promise.

You might also get this error if you forget to return an object from a function.

index.ts
const getObj = () => { const obj = { name: 'Tom' }; }; // ⛔️ Error: Property 'name' does // not exist on type 'void'.ts(2339) getObj().name

The solution is the same - you have to return the specific value from the function.

index.ts
const getObj = () => { const obj = { name: 'Tom' }; return obj; }; console.log(getObj().name); // 👉️ "Tom"

Sometimes it's hard to see where exactly the missing return statement is. In some cases you have many code paths (nested conditions, etc), but the error means that your function returns undefined, so you know what to look for.

A good solution when debugging is to explicitly type the function's return type.

index.ts
// ⛔️ Error: A function whose declared type // is neither 'void' nor 'any' must return a value.ts(2355) const getPromise = (): Promise<number> => { Promise.resolve(42); };

We explicitly typed the return value of the function to be Promise<number>, so TypeScript tells us that we aren't returning a value from the function.

This is an easy way to spot that you have a bug in your application.

All code paths of the function should return a value.

index.ts
// ⛔️ Function lacks ending return statement and // return type does not include 'undefined'.ts(2366) const getPromise = (): Promise<number> => { if (Math.random()) { return Promise.resolve(42); } Promise.resolve(33); };

We've explicitly typed the return type of the function to be Promise<number>, but we only return a promise in our if statement.

If the condition is not met, the if block will not run and the function will implicitly return undefined. To solve this, make sure to explicitly return a value from all of the function's code paths.

index.ts
const getPromise = (): Promise<number> => { if (Math.random()) { return Promise.resolve(42); } return Promise.resolve(33); };

Conclusion #

The "Property does not exist on type void" error occurs when we try to access a property on the return value of a function that doesn't return anything. To solve the error, make sure to return the correct value from all of the function's code paths.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.