From 97d51a7448c16c15938a2651084b61527d27d3e2 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 2 Feb 2026 09:11:49 -0800 Subject: [PATCH] Update implied default for module based on target (#63076) --- src/compiler/utilities.ts | 20 ++++++++++++++++--- .../awaitInNonAsyncFunction.errors.txt | 10 ++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 949c3dc01f8..a0d1c6e75d2 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -9000,9 +9000,23 @@ const _computedOptions = createComputedCompilerOptions({ module: { dependencies: ["target"], computeValue: (compilerOptions): ModuleKind => { - return typeof compilerOptions.module === "number" ? - compilerOptions.module : - _computedOptions.target.computeValue(compilerOptions) >= ScriptTarget.ES2015 ? ModuleKind.ES2015 : ModuleKind.CommonJS; + if (typeof compilerOptions.module === "number") { + return compilerOptions.module; + } + const target = _computedOptions.target.computeValue(compilerOptions); + if (target === ScriptTarget.ESNext) { + return ModuleKind.ESNext; + } + if (target >= ScriptTarget.ES2022) { + return ModuleKind.ES2022; + } + if (target >= ScriptTarget.ES2020) { + return ModuleKind.ES2020; + } + if (target >= ScriptTarget.ES2015) { + return ModuleKind.ES2015; + } + return ModuleKind.CommonJS; }, }, moduleResolution: { diff --git a/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt b/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt index b269d74966e..62dda6738b0 100644 --- a/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt +++ b/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt @@ -12,11 +12,9 @@ awaitInNonAsyncFunction.ts(30,9): error TS1103: 'for await' loops are only allow awaitInNonAsyncFunction.ts(31,5): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules. awaitInNonAsyncFunction.ts(34,7): error TS1103: 'for await' loops are only allowed within async functions and at the top levels of modules. awaitInNonAsyncFunction.ts(35,5): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules. -awaitInNonAsyncFunction.ts(39,5): error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher. -awaitInNonAsyncFunction.ts(40,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher. -==== awaitInNonAsyncFunction.ts (16 errors) ==== +==== awaitInNonAsyncFunction.ts (14 errors) ==== // https://github.com/Microsoft/TypeScript/issues/26586 function normalFunc(p: Promise) { @@ -96,8 +94,4 @@ awaitInNonAsyncFunction.ts(40,1): error TS1378: Top-level 'await' expressions ar } for await (const _ of []); - ~~~~~ -!!! error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher. - await null; - ~~~~~ -!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher. \ No newline at end of file + await null; \ No newline at end of file