-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Circular dependency #4149
Comments
stupid question.. are these functions instance or static? also what module loader are you using? |
@mhegazy Will using the ES6 Module syntax (#2242) prevent issues like this?
Does this apply to the |
Specifically, we have comments like this in our codebase:
It's very hard to predict when these issues will happen, since compilation succeeds with the import as an empty object, and things blow up at runtime. We're using browserify to bundle up our code, and we use the tsify plugin. |
@jiaweihli since you are using browserify i am assuming you are compileing with --module commonjs`. in this case, your output code will look something like: var B = require('services/B');
var A = (function () {
function A() {
}
A.prototype.run = function () {
B.run();
};
return A;
})();
module.exports = A; so it all depends on how your loader will handle circular dependencies at run time. i will need to play with the loader of a bit to get better understanding of what is going on. |
Seems to work correctly with ES6 modules (even circular static methods!) when using |
Migrated codebase over - it seems like using ES6 imports forces everything to be lazily evaluated, which solves the majority of circular import issues. However, given the scenario where you have a class |
@jiaweihli is still an issue? |
Yes, both for the case I mentioned above ^, and for the case of static getters, which are immediately evaluated. |
static getter/setter is by design. you can find more about it at #1520 (comment) |
Hitting same issue on Node.js, with the "export = A;" syntax. Example: class A { B.ts class B { C.ts |
I just had the same issue and it was a nightmare to figure out. While I do understand that the loaders may play part here, I wonder if at least the compiler should throw a warning? First I had this (using
I then changed the code to add a call from Function A to a function in Module C... and got This was all within Jasmine tests using |
Why is this issue closed when no answer to these issues has been posted??? I've been stuck with the same problem in Node JS for days now. I need to reference type A from B, and B from A. In pure JS, I would use runtime require and place "module.exports" at the beginning of the file, so Node can fill in the missing references later, as it resolves them. However, in Typescript I have no control over how and where "module.exports" is generated, so it always overwrites the whole object, causing unresolved modules. So what is the official workaround for Typescript files?? Not creating circular dependencies at all is NOT an option when models refer to each other (think foreign keys in a database). |
See #4149 (comment) for details. the above sample should be authored with ES6 syntax. this way you get the defered reference to your functions.: // a.ts
import { B } from './b';
export class A
{
static run()
{
B.run();
}
} // b.ts
import { A } from './a';
export class B
{
static run()
{
A.run();
}
} |
This is still a problem. I'm trying to figure out how to use ES6 syntax with |
I have this issue as well. It's such a basic thing to be able to need to do and TypeScript can't handle it. |
it seems like typescript should emit warnings if it's compiling to a module system that doesn't support cycles. |
@Spongeman haven't looked much into it, but these tslint-rules might help you. |
Looks like I have a circular dependency between three classes ('->' means 'references'): A -> B -> C -> A.
Class A:
Class B:
Class C:
Whenever I try to execute C.run in B I get exception: "TypeError: C.run is not a function" and in debug mode C is nothing, but an empty object.
Ok, I can redesign my classes to break the chain, but is it possible to have such references in TypeScript and what do I need to do?
The text was updated successfully, but these errors were encountered: