What to do when no export is needed in TypeScript file

I had a problem while making some demo files using TypeScript, each file is considered to run alone (no import or export needed).

The problem is that the files leaked to each other as they all went global (I'll appreciate if someone explains why this happened). I found a few ways to get rid of this as wrapping them in a module or a namespace, or even exporting an empty object.

What I need to know is the best practice that should be done in this situation? which solutions is considered the best? especially that I thought I can face the same situation if I have multiple files that are required for their side-effects only or something.

1 answer

  • answered 2019-06-11 22:54 basarat

    I had a problem while making some demo files using TypeScript

    What I need to know is the best practice that should be done in this situation? which solutions is considered the best? especially that I thought I can face the same situation if I have multiple files that are required for their side-effects only or something.

    The only time I've experienced it in my long career as well is with demo files. I had this when creating TypeScript deep dive so I would put in some junk at the top of the file e.g. see const

    export var asdfasdfasfadf = 123;
    

    Why its not a concern

    You do not see it happening in real world code because you start you brain with module mind set. E.g.

    • In a file with zero dependencies you are normally thinking : What am I going to export
    • In a file where you are going to action something you are normally thinking: What will I need to import. As simple as import fs from 'fs' makes it a module 🌹