Property 'push' does not exist on type in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 23, 2022

banner

Photo from Unsplash

Property 'push' does not exist on type in TypeScript #

The error "Property 'push' does not exist on type" occurs when we call the push() method on a value that isn't an array. To solve the error, make sure to only call the push() method on arrays or correct the type of the variable on which you call the method.

property push does not exist on type

Here are 2 examples of how the error occurs.

index.ts
const obj = {}; // ⛔️ Error: Property 'push' does not exist on type '{}'.ts(2339) obj.push('hello'); // --------------------------------------------------------------- // 👇️ it's an array, but has incorrect type const arr = ['one', 'two', 'three'] as unknown as { name: string }; // ⛔️ Property 'push' does not exist on type '{ name: string; }'.ts(2339) arr.push('four');

In the first example, we called the Array.push method on an object, which caused the error.

If you want to add a property to an object, make sure the object's type permits that and use dot or bracket notation.

index.ts
const obj: { name: string } = { name: '' }; // ⛔️ Error: Property 'push' does not exist on type '{}'.ts(2339) // obj.push('hello'); obj.name = 'Alice'; console.log(obj); // 👉️ {name: 'Alice'}

Otherwise, to start debugging, console.log the value you're calling the push method on and make sure it's an array.

index.ts
const obj: { name: string } = { name: '' }; console.log(Array.isArray(obj)); // 👉️ false console.log(Array.isArray([1, 2, 3])); // 👉️ true
If you determine that the value on which you're calling the push method is an array, try restarting your IDE and your development server. VSCode often glitches and needs a reboot.

If the value can sometimes be an object and other times an array, you have to use a type guard when calling the push method.

index.ts
const maybeArray = Math.random() > 0.5 ? [1, 2, 3] : { name: 'Tom' }; if (Array.isArray(maybeArray)) { maybeArray.push(4); console.log(maybeArray); // 👉️ [1, 2, 3, 4] }

The Array.isArray method serves as a type guard.

If the condition is met, TypeScript knows that the maybeArray variable stores an array and allows us to call the push() method.

Had we tried to call the method directly on the variable, we would have gotten the "Property 'push' does not exist on type" error, because of the possibility that the variable is not an array.

If you have concluded that the variable on which you're calling the push method is an array, you need to correct its type.

index.ts
// 👇️ it's an array, but has incorrect type const arr = ['one', 'two', 'three'] as unknown as { name: string }; // ⛔️ Property 'push' does not exist on type '{ name: string; }'.ts(2339) arr.push('four');

The arr variable stores an array, however, it has a different type so TypeScript won't let us call the push() method.

If you have no control over the type of the variable and know that it's an array, you can use a type assertion.

index.ts
const arr = ['one', 'two', 'three'] as unknown as { name: string }; (arr as unknown as any[]).push('four'); console.log(arr); // 👉️ ['one', 'two', 'three', 'four']

Type assertions are used when we have information about the type of a value that TypeScript can't know about.

We effectively tell TypeScript that the arr variable will be of type any[] and not to worry about it.

If this is the cause of the error in your case, it's best to figure out where the incorrect type stems from.

Conclusion #

The error "Property 'push' does not exist on type" occurs when we call the push() method on a value that isn't an array. To solve the error, make sure to only call the push() method on arrays or correct the type of the variable on which you call the method.

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.