skip to content

You lead a legacy TypeScript codebase built on `namespace App.Utils`, triple-slash references and `outFile`, while a global library's typings still use namespaces; how do you decide whether and when it moves to ES modules?

level: principalimportance: should knowfreq 18%

answer

  1. what forces the move, and when
  2. who reads the global from outside
  3. one import changes the whole file
  4. bridge with a dated removal
  5. global library typings stay put

basics

~20 s

Weigh forcing events, such as outFile's deprecation in TypeScript 6.0 and any move to bundling or strip-only tools, against how much the code still changes. Then sequence carefully: one import turns a namespace file into a module and detaches it.

solid answer

~50 s

I would start with what forces the move. TypeScript 6.0 reports `outFile` as deprecated, with a message saying it stops functioning in 7.0, so a concatenated build has a deadline unless you pin the compiler. Wanting bundling, code splitting or strip-only execution also pushes that way. Against that I weigh how much the code still changes, its test coverage, and who reads `App.Utils` from outside TypeScript, such as inline scripts. Frozen code may justify staying pinned for a while, with the risk written down. Sequencing is the hard part, because adding one `import` turns a file into a module and its `namespace App` block stops merging with the global one. So I either convert in one coordinated pass or work from the entry points inward, with modules reading the remaining globals. A temporary global bridge gets a removal date. The global library's `declare namespace` typings stay, since they emit nothing.

code

typescript · 10 lines
typescript
// validation.ts
namespace App.Utils {
  export const salt = 'x';
}

// hash.ts
/// <reference path="validation.ts" />
namespace App.Utils {
  export function hash(s: string) { return salt + s; }
}

go deeper

for a junior

Recall what the old setup is: shared namespaces, reference tags and a concatenated file, with one global object at the center.

for a middle

Explain the mechanics of converting a file: exports move to the top level, and bare names the compiler used to rewrite become imports.

for a senior

Show the trap: an added import makes a file a module and detaches its namespace, so a file-by-file plan needs an order or a bridge.

for a principal

Weigh deadlines from the compiler and delivery needs against code churn and remaining lifetime, name the trigger for any delay, and set a measurable end state.

## The starting point The codebase in question has three connected features. Files contribute to shared **namespaces** such as `App.Utils`. `/// <reference path="..." />` tags tell the compiler how the files relate. `outFile` concatenates them into one script, in the order the tags imply. The whole design depends on a single global object and a single load order, so this is one architectural decision and not hundreds of small ones. ## What forces the decision There is no universally correct answer, but some facts move the date: - **Compiler support.** The handbook still documents `outFile`, but TypeScript 6.0 reports TS5101: "Option 'outFile' is deprecated and will stop functioning in TypeScript 7.0", which `"ignoreDeprecations": "6.0"` silences. `module: none` gets the same treatment as TS5107. A concatenated build now has a clock on it, or a pinned compiler. - **Delivery needs.** Code splitting and dropping unused code rely on an import graph, and the handbook itself says modules have better tooling support for bundling. - **Execution model.** Tools that only strip types refuse value namespaces, which closes off that route. - **Hiring and maintenance.** Hidden dependencies make every change slower, and the pattern is unfamiliar to many newer engineers. ## What argues for waiting | Situation | Reasonable position | |---|---| | Code is frozen and rarely changes | stay pinned, with the risk recorded | | Application is being retired soon | do not migrate | | Few tests cover load behaviour | build characterization tests first | | Active feature work on a large code base | migrate incrementally | | Small, cohesive code base | a single scripted conversion | Pinning is a real choice, but the cost accrues: no compiler upgrades, and eventually no tool upgrades. Say so explicitly, and name the trigger that reopens the decision. ## Why sequencing is the hard part The trap that shapes the plan is this. A file with a top-level `import` or `export` is a module, and a namespace inside it is local to that file: ```ts // x.ts: this import makes the file a module import { y } from './y'; namespace V { export const a = y; } // z.ts: still a script, so its V is a different one namespace V { export const b = 1; } console.log(V.a); // TS2339: Property 'a' does not exist ``` You cannot convert one file by adding an import and leave it contributing to the global. And a script file cannot use a static `import` at all. A module, however, can read a global namespace as long as that script has loaded first. That asymmetry gives the options: 1. **One coordinated pass.** A scripted rewrite in one change. This is the cleanest result when the code is small enough and the team can pause. 2. **Entry points inward.** Convert the top-level files first so they can import, and let them read the remaining globals until those convert too. 3. **Leaves first, with a bridge.** Convert a leaf to a module and assign it to the global for the scripts that remain. The bridge is a rudiment and needs a written removal date. ## What the conversion itself must handle - **Unqualified references.** The compiler rewrote a bare `key` to `Utils.key` across blocks, so each of those becomes an explicit import. - **Load-order assumptions.** Top-level side effects relied on script order, and an import graph may expose cycles that were previously hidden. - **Needless namespacing.** Do not write `export namespace Shapes` in the new file, which forces `shapes.Shapes.Triangle` on consumers. - **Merged statics.** A namespace merged with a function, class or enum needs a rewrite into statics or plain functions. - **Outside readers.** Inline scripts or other pages reading `App.Utils` need the bridge or their own change. ## What stays, and when it is finished The global library's `declare namespace` typings are not part of this work. They emit nothing and describe an object that still exists at runtime. Migrating your code leaves them correct, so rewriting them is churn with no gain. Set a measurable end state: no `/// <reference path>` tags left in application code, no `outFile` or `ignoreDeprecations`, and `erasableSyntaxOnly` turned on in CI (available since TypeScript 5.8) so value namespaces cannot return. Track the count of remaining namespace files, so the migration stays visible and does not stall halfway, which is the worst place to stop, as it leaves two systems and a bridge to maintain.

  • During a TypeScript namespace migration, why does adding one `import` to a namespace file break other files that compiled yesterday?
    The `import` turns the file into a module, so its `namespace App` block becomes local to it and no longer merges with the global `App` in other files. Those files then report TS2339 for members that moved out of reach. Plan around it by converting in one pass, or by converting entry points first.
  • What does a temporary global bridge look like in a TypeScript namespace migration, and what keeps it from becoming permanent?
    A converted leaf is imported by a small file that assigns it to the old global name, so remaining scripts keep working. That file is a rudiment: record its removal trigger, such as the last script reader converting, and count remaining readers so the end date is visible.
  • How do you protect a TypeScript codebase from regressions in load behaviour before such a migration starts?
    Write characterization tests against the built output, running the real page or script sequence and checking that key global members exist and behave. Type checks cannot see this, because the compiler does not check script-tag order. They also tell you whether a converted module still runs its side effects at the expected time.

  • Every namespace codebase must migrate immediately, whatever its remaining lifetime.
  • You can convert one file at a time by adding imports, with no other change.
  • The global library's declare namespace typings have to be rewritten as part of the migration.
  • After converting, wrap each module's exports in a namespace so call sites keep their shape.
  • Reference tags can stay in place to enforce import order after the move to modules.

Assumes TypeScript 6.0, whose compiler reports outFile as deprecated (TS5101); the handbook pages still document outFile without that warning.