Skip to content

Commit 241f54e

Browse files
author
Andy Hanson
committed
Don't allow to re-export a type when '--isolatedModules' is set
1 parent 1db4f96 commit 241f54e

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21189,6 +21189,11 @@ namespace ts {
2118921189
Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0;
2119021190
error(node, message, symbolToString(symbol));
2119121191
}
21192+
21193+
// Don't allow to re-export something with no value side when `--isolatedModules` is set.
21194+
if (node.kind === SyntaxKind.ExportSpecifier && compilerOptions.isolatedModules && !(target.flags & SymbolFlags.Value)) {
21195+
error(node, Diagnostics.Cannot_re_export_a_type_when_the_isolatedModules_flag_is_provided);
21196+
}
2119221197
}
2119321198
}
2119421199

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,10 @@
635635
"category": "Error",
636636
"code": 1203
637637
},
638+
"Cannot re-export a type when the '--isolatedModules' flag is provided.": {
639+
"category": "Error",
640+
"code": 1205
641+
},
638642
"Decorators are not valid here.": {
639643
"category": "Error",
640644
"code": 1206
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/user.ts(2,10): error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided.
2+
/user.ts(17,10): error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided.
3+
4+
5+
==== /user.ts (2 errors) ====
6+
// Error, can't re-export something that's only a type.
7+
export { T } from "./exportT";
8+
~
9+
!!! error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided.
10+
export import T2 = require("./exportEqualsT");
11+
12+
// OK, has a value side
13+
export { C } from "./exportValue";
14+
15+
// OK, even though the namespace it exports is only types.
16+
import * as NS from "./exportT";
17+
export { NS };
18+
19+
// OK, syntactically clear that a type is being re-exported.
20+
export type T3 = T;
21+
22+
// Error, not clear (to an isolated module) whether `T4` is a type.
23+
import { T } from "./exportT";
24+
export { T as T4 };
25+
~~~~~~~
26+
!!! error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided.
27+
28+
==== /exportT.ts (0 errors) ====
29+
export type T = number;
30+
31+
==== /exportValue.ts (0 errors) ====
32+
export class C {}
33+
34+
==== /exportEqualsT.ts (0 errors) ====
35+
declare type T = number;
36+
export = T;
37+
38+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//// [tests/cases/compiler/isolatedModulesReExportType.ts] ////
2+
3+
//// [exportT.ts]
4+
export type T = number;
5+
6+
//// [exportValue.ts]
7+
export class C {}
8+
9+
//// [exportEqualsT.ts]
10+
declare type T = number;
11+
export = T;
12+
13+
14+
//// [user.ts]
15+
// Error, can't re-export something that's only a type.
16+
export { T } from "./exportT";
17+
export import T2 = require("./exportEqualsT");
18+
19+
// OK, has a value side
20+
export { C } from "./exportValue";
21+
22+
// OK, even though the namespace it exports is only types.
23+
import * as NS from "./exportT";
24+
export { NS };
25+
26+
// OK, syntactically clear that a type is being re-exported.
27+
export type T3 = T;
28+
29+
// Error, not clear (to an isolated module) whether `T4` is a type.
30+
import { T } from "./exportT";
31+
export { T as T4 };
32+
33+
34+
//// [exportT.js]
35+
"use strict";
36+
exports.__esModule = true;
37+
//// [exportEqualsT.js]
38+
"use strict";
39+
exports.__esModule = true;
40+
//// [exportValue.js]
41+
"use strict";
42+
exports.__esModule = true;
43+
var C = (function () {
44+
function C() {
45+
}
46+
return C;
47+
}());
48+
exports.C = C;
49+
//// [user.js]
50+
"use strict";
51+
exports.__esModule = true;
52+
// OK, has a value side
53+
var exportValue_1 = require("./exportValue");
54+
exports.C = exportValue_1.C;
55+
// OK, even though the namespace it exports is only types.
56+
var NS = require("./exportT");
57+
exports.NS = NS;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @isolatedModules: true
2+
3+
// @Filename: /exportT.ts
4+
export type T = number;
5+
6+
// @Filename: /exportValue.ts
7+
export class C {}
8+
9+
// @Filename: /exportEqualsT.ts
10+
declare type T = number;
11+
export = T;
12+
13+
14+
// @Filename: /user.ts
15+
// Error, can't re-export something that's only a type.
16+
export { T } from "./exportT";
17+
export import T2 = require("./exportEqualsT");
18+
19+
// OK, has a value side
20+
export { C } from "./exportValue";
21+
22+
// OK, even though the namespace it exports is only types.
23+
import * as NS from "./exportT";
24+
export { NS };
25+
26+
// OK, syntactically clear that a type is being re-exported.
27+
export type T3 = T;
28+
29+
// Error, not clear (to an isolated module) whether `T4` is a type.
30+
import { T } from "./exportT";
31+
export { T as T4 };

0 commit comments

Comments
 (0)