diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 34a176ac23c28..f55feaf87eddc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21189,6 +21189,11 @@ namespace ts { Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0; error(node, message, symbolToString(symbol)); } + + // Don't allow to re-export something with no value side when `--isolatedModules` is set. + if (node.kind === SyntaxKind.ExportSpecifier && compilerOptions.isolatedModules && !(target.flags & SymbolFlags.Value)) { + error(node, Diagnostics.Cannot_re_export_a_type_when_the_isolatedModules_flag_is_provided); + } } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4d2997ba3a820..41936c0cc6a5c 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -635,6 +635,10 @@ "category": "Error", "code": 1203 }, + "Cannot re-export a type when the '--isolatedModules' flag is provided.": { + "category": "Error", + "code": 1205 + }, "Decorators are not valid here.": { "category": "Error", "code": 1206 diff --git a/tests/baselines/reference/isolatedModulesReExportType.errors.txt b/tests/baselines/reference/isolatedModulesReExportType.errors.txt new file mode 100644 index 0000000000000..10c07cde084e3 --- /dev/null +++ b/tests/baselines/reference/isolatedModulesReExportType.errors.txt @@ -0,0 +1,38 @@ +/user.ts(2,10): error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided. +/user.ts(17,10): error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided. + + +==== /user.ts (2 errors) ==== + // Error, can't re-export something that's only a type. + export { T } from "./exportT"; + ~ +!!! error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided. + export import T2 = require("./exportEqualsT"); + + // OK, has a value side + export { C } from "./exportValue"; + + // OK, even though the namespace it exports is only types. + import * as NS from "./exportT"; + export { NS }; + + // OK, syntactically clear that a type is being re-exported. + export type T3 = T; + + // Error, not clear (to an isolated module) whether `T4` is a type. + import { T } from "./exportT"; + export { T as T4 }; + ~~~~~~~ +!!! error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided. + +==== /exportT.ts (0 errors) ==== + export type T = number; + +==== /exportValue.ts (0 errors) ==== + export class C {} + +==== /exportEqualsT.ts (0 errors) ==== + declare type T = number; + export = T; + + \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModulesReExportType.js b/tests/baselines/reference/isolatedModulesReExportType.js new file mode 100644 index 0000000000000..545e5a81a8a88 --- /dev/null +++ b/tests/baselines/reference/isolatedModulesReExportType.js @@ -0,0 +1,57 @@ +//// [tests/cases/compiler/isolatedModulesReExportType.ts] //// + +//// [exportT.ts] +export type T = number; + +//// [exportValue.ts] +export class C {} + +//// [exportEqualsT.ts] +declare type T = number; +export = T; + + +//// [user.ts] +// Error, can't re-export something that's only a type. +export { T } from "./exportT"; +export import T2 = require("./exportEqualsT"); + +// OK, has a value side +export { C } from "./exportValue"; + +// OK, even though the namespace it exports is only types. +import * as NS from "./exportT"; +export { NS }; + +// OK, syntactically clear that a type is being re-exported. +export type T3 = T; + +// Error, not clear (to an isolated module) whether `T4` is a type. +import { T } from "./exportT"; +export { T as T4 }; + + +//// [exportT.js] +"use strict"; +exports.__esModule = true; +//// [exportEqualsT.js] +"use strict"; +exports.__esModule = true; +//// [exportValue.js] +"use strict"; +exports.__esModule = true; +var C = (function () { + function C() { + } + return C; +}()); +exports.C = C; +//// [user.js] +"use strict"; +exports.__esModule = true; +// OK, has a value side +var exportValue_1 = require("./exportValue"); +exports.C = exportValue_1.C; +// OK, even though the namespace it exports is only types. +var NS = require("./exportT"); +exports.NS = NS; diff --git a/tests/cases/compiler/isolatedModulesReExportType.ts b/tests/cases/compiler/isolatedModulesReExportType.ts new file mode 100644 index 0000000000000..d1e05af6c8320 --- /dev/null +++ b/tests/cases/compiler/isolatedModulesReExportType.ts @@ -0,0 +1,31 @@ +// @isolatedModules: true + +// @Filename: /exportT.ts +export type T = number; + +// @Filename: /exportValue.ts +export class C {} + +// @Filename: /exportEqualsT.ts +declare type T = number; +export = T; + + +// @Filename: /user.ts +// Error, can't re-export something that's only a type. +export { T } from "./exportT"; +export import T2 = require("./exportEqualsT"); + +// OK, has a value side +export { C } from "./exportValue"; + +// OK, even though the namespace it exports is only types. +import * as NS from "./exportT"; +export { NS }; + +// OK, syntactically clear that a type is being re-exported. +export type T3 = T; + +// Error, not clear (to an isolated module) whether `T4` is a type. +import { T } from "./exportT"; +export { T as T4 };