Home > Enterprise >  Typescript import/export
Typescript import/export

Time:06-21

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 }

CodePudding user response:

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

  • Related