Tuesday, December 5, 2017

TypeScript: Variable 'require' must be of type 'any', but here has type 'NodeRequire'.

After loading a solution for a code review, I was welcomed by the following error message:

Variable 'require' must be of type 'any', but here has type 'NodeRequire'.

Google brought me to the following GitHub issue; https://github.com/Microsoft/TypeScript/issues/16298 where the proposed solution was as simple as beautiful:

There must be another definition of require somewhere.

And indeed, after triggering a search on my solution I found another ‘declare var require’ somewhere in a typedefinition file. After removing it, the error disappeared (and everything still worked).

No comments: