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.