Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict between RequireJs and NodeJS definitions in Typescript

I got the Typescript RequireJS definition from Definitely Typed - which has an ambient declaration of Require that conflicts with the NodeJs command "require". Here's the declaration and the error:

Declaration:

declare var require: Require;

Error:

C:/.../require.d.ts(320,13): error TS2134: Subsequent variable declarations must have the same type.  Variable 'require' must be of type '{ resolve(id: string): string; cache: any; extensions: any; main: any; (id: string): any; }', but here has type 'Require'.
like image 796
EternallyCurious Avatar asked Jan 27 '14 12:01

EternallyCurious


2 Answers

That is true. What I had to do was simply use declare var require:any and not reference nodejs or RequireJS.

Would appreciate if you can open a bug here : https://github.com/borisyankov/DefinitelyTyped

Reason why it's tricky is because you don't actually have both node and RequireJS in the same environment. It's a compile time reuse thing we need to support.

like image 125
basarat Avatar answered Oct 16 '22 01:10

basarat


I had this issue while trying to integrate monaco-editor in my application. Workaround is pretty simple.

  1. Insert this line <script>window.requirejs = window.require;</script> after included require.js or loader.js in your html.

  2. Then in your .ts file where you want to use requirejs, replace all require with requirejs. This alias is already defined in require.d.ts

  3. Done!

like image 24
SomeBruh Avatar answered Oct 16 '22 02:10

SomeBruh