ruk·si

🏷️ TypeScript
Architecture

Updated at 2024-08-02 02:57
  • Module is a file with a top-level export or import.
  • Script is any file that is not a module.

Thus, you can force a file to be a module with export {};

Scripts are merged to a single "namespace". That means variables of the same name conflict with each other as if they were declared in the same file:

// a.ts
const shared = "Cher";
// Cannot redeclare block-scoped variable 'shared'.

// b.ts
const shared = "Cher";
// Cannot redeclare block-scoped variable 'shared'.

This is super useful in the browser context:

interface Window { 
    myVariable: string;
}
window.myVariable = "don't deliver secrets like this, though";

Sources

  • Learning TypeScript by Josh Goldberg