
Imagine trying to build a complex machine, but all your parts are scattered across the floor, and there's no clear instruction manual for how they connect. That's a bit like early JavaScript development. For a long time, the language lacked a built-in, standardized way for different files to share code cleanly. Developers often relied on clever (and sometimes brittle) workarounds to manage dependencies and avoid naming conflicts.
That all changed relatively recently! With ECMAScript 2015 (ES2015), a pivotal update brought us "ECMAScript modules," or ESM. This was a game-changer, standardizing the import and export syntax we commonly use today to explicitly define what a file makes available to others and what it needs from them.
Think of it like this:
A Module is a file that's a self-contained unit. It explicitly declares what it's offering to the world (export) and what it needs from other units (import). If something isn't exported, it stays private to that file, preventing accidental name clashes.
A Script is a file that's globally scoped. Everything in a script is potentially accessible everywhere else in your application, which can lead to frustrating naming collisions when projects grow.

In this setup, initialValue is clearly exported from values.ts, and doubledValue is clearly exported from calculator.ts. It's like having well-defined interfaces between different components of your machine.
TypeScript fully embraces modern modules and even provides helpful compile-time checks based on whether a file is a module or a script.
When files are treated as modules, anything declared inside them is private unless explicitly exported. This means you can have variables with the same name in different modules without any issues, because their scopes are isolated. It's like having two different houses, each with a "shared" living room they're both called "shared," but they belong to different houses, so there's no confusion.
Let's look at a.ts and b.ts, both acting as modules by virtue of their export statements:

Result: Perfectly fine! TypeScript understands that these shared variables belong to their respective modules, preventing any global naming conflicts.
However, things get interesting when you try to import something and declare something with the same name in the same file:
