Typescript + requirejs: How to handle circular dependencies?

Posted by Aymeric Gaurat-Apelli on Stack Overflow See other posts from Stack Overflow or by Aymeric Gaurat-Apelli
Published on 2014-06-08T09:20:02Z Indexed on 2014/06/08 9:24 UTC
Read the original article Hit count: 291

Filed under:
|

I am in the process of porting my JS+requirejs code to typescript+requirejs. One scenario I haven't found how to handle is circular dependencies.

Require.js returns undefined on modules that are also dependent on the current and to solve this problem you can do:

MyClass.js

define(["Modules/dataModel"], function(dataModel){
  return function(){
    dataModel = require("Modules/dataModel");
    ...
  }
});

Now in typescript, I have:

MyClass.ts

import dataModel = require("Modules/dataModel");

class MyClass {

  dataModel: any;

  constructor(){

    this.dataModel = require("Modules/dataModel"); // <- this kind of works but I lose typechecking
    ...
  }
}

How to call require a second time and yet keep the type checking benefits of typescript? dataModel is a module { ... }

© Stack Overflow or respective owner

Related posts about requirejs

Related posts about TypeScript