0

I'm a newbie to typescript

Currently i have 2 files contains 2 classes which import each others utils which has s3 class as a property

Utils.ts:

import { S3Storage } from '../storage/s3';

export class Utils {
    static s3Writer = new S3Storage(env.buckerName);

    static async UtilsMethod(){

    };

}

and s3 which use utils class method

S3.ts:

import { Utils } from '../util/utils';

export class S3Storage {
    constructor(bucketName) {
      const AWS_ID = process.env.AWS_ACCESS_KEY_ID;
      const AWS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
      const AWS_REGION = process.env.AWS_REGION;
      
      const AWS_ID_VIEW = AWS_ID.substring(AWS_ID.length - 4, AWS_ID.length);
      const AWS_KEY_VIEW = AWS_KEY.substring(AWS_KEY.length - 4, AWS_KEY.length);

      this.logger.info(`Setting up s3PageWriter to bucket: ${bucketName}`);
      this.logger.info(`ID: ${AWS_ID_VIEW}`);
      this.logger.info(`Key: ${AWS_KEY_VIEW}`);
      AWS.config.apiVersions = { s3: '2006-03-01' };
      AWS.config.update({
         region: AWS_REGION,
      });

      this.bucketName = bucketName;
      this.s3 = new AWS.S3({
          accessKeyId: AWS_ID,
          secretAccessKey: AWS_KEY,
      });
     }
    async s3Method(){
      Utils.UtilsMethod();
    }
}

EDIT: the first time i though it's circular import because it work when i removed the import Utils from my s3 but now i don't think that the reason. I got this error before if i tried to run from the first example

export class Utils {
^
TypeError: s3_1.S3Storage is not a constructor

even though my s3 class has constructor

export class S3Storage {
  logger = require('../util/log')(this.constructor.name);
  bucketName: string;
  s3;

  constructor(bucketName) {
     ...
  }
}
Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67
  • Circular imports are fine, provided you don't try to *use* anything in the cycle until it's resolved (which your code doesn't, so that's fine). (This is an important part of the design of JavaScript's standard module system.) The answer you linked is resolving a circular type definition issue, but you don't have a circular type definition above. What problem are you solving by adding the `declare`? – T.J. Crowder Jan 26 '22 at 10:16
  • That answer only refers to circular dependency on type imports. You are using the imports as values, so the solution won't work for you – Jason Jan 26 '22 at 10:18
  • @T.J.Crowder i would like to use method from Utils class, but when i used import type i can't call Utils.UtilsMethod() like so it showing lint error as `'Utils' cannot be used as a value because it was imported using 'import type'` . So i used declare on it but i guess that not how it's used – Linh Nguyen Jan 26 '22 at 10:19
  • @Jason so is there other way that i can avoid circular import without using `import type` ? – Linh Nguyen Jan 26 '22 at 10:20
  • An important thing to note in your question: You're compiling to CommonJS, even though you're using native module syntax. That matters, because CommonJS modules don't handle cycles as well as JavaScript native modules do, so I'd highlight it in the question. – T.J. Crowder Jan 26 '22 at 10:28
  • @T.J.Crowder thank, i updated the question, now i don't think it's related to circular import – Linh Nguyen Jan 26 '22 at 10:33

0 Answers0