Last updated: Feb 27, 2024
Reading timeยท3 min
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:
const getPromise = () => { Promise.resolve(42); }; // โ๏ธ Error: Property 'then' does not exist on type 'void'.ts(2339) getPromise().then((value) => { console.log(value); });
void
The getPromise()
function doesn't return a value so TypeScript sets its return
type to be
void.
// ๐๏ธ 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.
// ๐๏ธ const getPromise: () => Promise<number> const getPromise = () => { return Promise.resolve('bobbyhadz.com'); }; // โ Works getPromise().then((value) => { console.log(value); // ๐๏ธ "bobbyhadz.com" });
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.
const getObj = () => { const obj = { name: 'Bobby Hadz' }; }; // โ๏ธ Error: Property 'name' does not exist on type 'void'.ts(2339) getObj().name;
The solution is the same - you have to return the value from the function.
const getObj = () => { const obj = { name: 'Bobby Hadz' }; return obj; }; console.log(getObj().name); // ๐๏ธ "Bobby Hadz"
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 set the function's return type.
// โ๏ธ 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.
// โ๏ธ 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 isn't met, the if
block won't run and the function will
implicitly return undefined
.
To resolve this, make sure to explicitly return a value from all of the function's code paths.
const getPromise = (): Promise<number> => { if (Math.random()) { return Promise.resolve(42); } return Promise.resolve(33); };
You can learn more about the related topics by checking out the following tutorials: