Skip to content

isolatedModules acts like moduleDetection: "force" #54094

@wnayes

Description

@wnayes

Bug Report

With TypeScript 5.0, issue #46295 was addressed by @andrewbranch and the compiler no longer considers it a build error to have a global script in a project with isolatedModules: true.

However, I was surprised that isolatedModules: true still forces all scripts to emit in module format.

This can be seen in the TypeScript Playground. The following example emits an AMD module, even though the script contents do not contain export/import and moduleDetection was set to auto.

// @module:amd
// @moduleDetection:auto
// @isolatedModules:true
console.log("Hello world!");

https://www.typescriptlang.org/play?module=2&moduleDetection=2&isolatedModules=true&ssl=4&ssc=29&pln=1&pc=1#code/PTAEAEFsHsBMFcA2BTAXAQ0rAUCCMEUARZAF2QGNSBLaAOw3lOlzHGoGdpF1zYBZOEmQdUpAE7xk2CvS4oAdImgBzABQAiABLJEy0AHdo4xLACEGgJQBuIA

I expected to be able to control the module emit via moduleDetection. I didn't expect isolatedModules: true to act like moduleDetection: "force".

The use case I was hoping to implement was setting isolatedModules: "true" on a project that is mostly modules, but has a "bootstrap" script that is a non-module, and use transpileModule to emit. I found that the bootstrap script emitted as a module, which failed to work at runtime.

If transpileModule can't safely emit global scripts, and there is not interest in supporting such emit, it might be more intuitive if setting isolatedModules: true also demanded that moduleDetection: "force" be explicitly set.

If transpileModule can safely emit global scripts, ideally isolatedModules: true would stop forcing module emit, leaving the decision to moduleDetection.

🕗 Version & Regression Information

This changed between versions 4.9 and 5.0. (Or rather, the new behavior in 5.0 makes this behavior apparent. Before it didn't matter that isolatedModules forced module format emit, because it also rejected non-modules outright.)

🙁 Actual behavior

From the playground link above,

define(["require", "exports"], function (require, exports) {
    "use strict";
    // @module:amd
    // @moduleDetection:auto
    // @isolatedModules:true
    console.log("Hello world!");
});

🙂 Expected behavior

Expected no AMD module wrapper:

"use strict";
// @module:amd
// @moduleDetection:auto
// @isolatedModules:false
console.log("Hello world!");

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions