1

I have a regular nodejs backend with some js files. I also have a ts module that is still in development. I put ts files into the backend. And each time I update the ts module, I am running tsc; nodemon src/index.js. But compiling ts codes each time, examining generated js codes and then finding things then using it in js file is started to get annoying.

So, the question is, is it possible to use directly the ts file itself without compiling it each time?
I know it has to be compiled to run but Is it not possible to reference directly ts variables in js file with some npm package or somethingconfig.json file?, I mean I guess what I am doing is not the way it should be, ain't I right? I need a faster and modern way.

Thanks in advance.

To clarify the question here are some screenshots.

The ts module:
enter image description here

code from some js file:
enter image description here

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",  
    "module": "es2020",  
    "strict": true,  
    "esModuleInterop": true,  
    "skipLibCheck": true,  
    "forceConsistentCasingInFileNames": true  
  }
}
Muhammed Aydogan
  • 570
  • 1
  • 4
  • 22
  • A ts file or module has to be compiled to be used. But, it only has to be compiled once after it has been changed, it doesn't have to be compiled again if it hasn't been modified. There are many modules on NPM that are originally written in TypeScript. They are compiled to generate an interface in plain Javascript and then anyone can use them without again compiling them by using that compiled code. You can do something similar with your TypeScript code. But, anytime you modify your TypeScript code, you will have to compile it again. – jfriend00 Apr 08 '21 at 13:26
  • You cannot run the TypeScript file directly in node.js. It has to be compiled into Javascript before node.js can run it. – jfriend00 Apr 08 '21 at 13:27
  • I know it has to be compiled. Is it not possible referencing directly ts variables in js file with some npm package or some config file? @jfriend00 – Muhammed Aydogan Apr 08 '21 at 13:31
  • My recommendation would be to make your TS code into a module with a set, defined public interface. Then, you are defining (in JS) the public interface and that will not change from time to time. You can define that interface several ways, including using the `interface` features of TypeScript which you can read about [here](https://blog.logrocket.com/interfaces-in-typescript-what-are-they-and-how-do-we-use-them-befbc69b38b3/). – jfriend00 Apr 08 '21 at 13:40
  • I really appreciate your help. Actually, the ts module itself is an assignment of the Desing Patterns lecture. Using interfaces is a must for Adapter Patterns. So, I am using interfaces already. But there are other features for many different endpoints. I don't think I can use interfaces for all of them :D. I guess this problem will grow as I expand the module. – Muhammed Aydogan Apr 08 '21 at 13:58

0 Answers0