0

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 { ... }

1 Answer 1

2

Specify the type using what you get from import i.e

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

class MyClass {

  dataModel: typeof dataModelType;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.