Categories
TypeScript Answers

How to fix the “Cannot read property ‘push’ of undefined in [null]” error with TypeScript?

Spread the love

Sometimes, we want to fix the "Cannot read property ‘push’ of undefined in [null]" error with TypeScript.

In this article, we’ll look at how to fix the "Cannot read property ‘push’ of undefined in [null]" error with TypeScript.

How to fix the "Cannot read property ‘push’ of undefined in [null]" error with TypeScript?

To fix the "Cannot read property ‘push’ of undefined in [null]" error with TypeScript, we need to make sure the variable we call push on is an array.

For instance, we write

const stringArr: Array<string> = [];

or

const stringArr: string[] = [];

to declare the stringArr array.

Then we can call stringArr.push without errors.

Conclusion

To fix the "Cannot read property ‘push’ of undefined in [null]" error with TypeScript, we need to make sure the variable we call push on is an array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *