2

After running app.ts, I was expecting to get this result :

{ val: 'A' } //C
{ val: 'CONSTANT'} //B

but, I'm getting this result:

{ val: 'A' } //C
{ val: undefined } //B supposed to be 'CONSTANT' ??

Any ideas?? thank you !

This is the complete code:

tsconfig.json

"target": "es6",                                     
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist", 
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,    
"strictPropertyInitialization": false,
"skipLibCheck": true 

app.ts

import { C } from "./second";
import { B } from "./first";

console.log(C)
console.log(B)

/**
 *  C depends on A
 *  B depends on CONSTANT
 **/

first.ts

import { CONSTANT } from "./second"
const A ={
    val: 'A'
}
const B = {
    val: CONSTANT
}
export { A, B }

second.ts

import { A } from "./first";
const CONSTANT = 'CONSTANT'
const C = {
    val: A.val
}
export { CONSTANT, C }

1 Answer 1

2

Here is a cyclic dependency. const CONSTANT = 'CONSTANT' should be placed in a separate file

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. but I dont understand why it's considered as cyclic dependency while const C depends only on const A and const B depends only on const CONSTANT.
The JS engine reads files line by line. 1) app.ts -> import { C } from "./second"; 2) second.ts -> import { A } from "./first"; 3) import { CONSTANT } from "./second" Here he understands that the second.ts file has already been imported earlier, so he takes it from memory, but since he has not yet read it to the end (he only counted the first line), he takes CONSTANT as undefined
Appreciate, thank you for the clarification !
It does not work for me. Simplified the case, stackoverflow.com/questions/73594930/…

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.