Skip to content

Commit 20e8eba

Browse files
authored
Issue better error for exporting primitive type (microsoft#36424)
* Issue better error for exporting primitive type * Delete nonsense
1 parent 3054500 commit 20e8eba

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,7 @@ namespace ts {
17921792
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg!) && // TODO: GH#18217
17931793
!checkAndReportErrorForExtendingInterface(errorLocation) &&
17941794
!checkAndReportErrorForUsingTypeAsNamespace(errorLocation, name, meaning) &&
1795+
!checkAndReportErrorForExportingPrimitiveType(errorLocation, name) &&
17951796
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) &&
17961797
!checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation, name, meaning) &&
17971798
!checkAndReportErrorForUsingValueAsType(errorLocation, name, meaning)) {
@@ -2053,9 +2054,21 @@ namespace ts {
20532054
return false;
20542055
}
20552056

2057+
function isPrimitiveTypeName(name: __String) {
2058+
return name === "any" || name === "string" || name === "number" || name === "boolean" || name === "never" || name === "unknown";
2059+
}
2060+
2061+
function checkAndReportErrorForExportingPrimitiveType(errorLocation: Node, name: __String): boolean {
2062+
if (isPrimitiveTypeName(name) && errorLocation.parent.kind === SyntaxKind.ExportSpecifier) {
2063+
error(errorLocation, Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, name as string);
2064+
return true;
2065+
}
2066+
return false;
2067+
}
2068+
20562069
function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
20572070
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule)) {
2058-
if (name === "any" || name === "string" || name === "number" || name === "boolean" || name === "never") {
2071+
if (isPrimitiveTypeName(name)) {
20592072
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, unescapeLeadingUnderscores(name));
20602073
return true;
20612074
}
@@ -33300,6 +33313,7 @@ namespace ts {
3330033313
const id = node.expression as Identifier;
3330133314
const sym = resolveEntityName(id, SymbolFlags.All, /*ignoreErrors*/ true, /*dontResolveAlias*/ true, node);
3330233315
if (sym) {
33316+
3330333317
markAliasReferenced(sym, id);
3330433318
// If not a value, we're interpreting the identifier as a type export, along the lines of (`export { Id as default }`)
3330533319
const target = sym.flags & SymbolFlags.Alias ? resolveAlias(sym) : sym;

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@
11271127
"category": "Error",
11281128
"code": 1378
11291129
},
1130+
11301131
"The types of '{0}' are incompatible between these types.": {
11311132
"category": "Error",
11321133
"code": 2200
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/externalModules/exportNonLocalDeclarations.ts(1,10): error TS2661: Cannot export 'string'. Only local declarations can be exported from a module.
2+
tests/cases/conformance/externalModules/exportNonLocalDeclarations.ts(2,15): error TS2661: Cannot export 'number'. Only local declarations can be exported from a module.
3+
4+
5+
==== tests/cases/conformance/externalModules/exportNonLocalDeclarations.ts (2 errors) ====
6+
export { string };
7+
~~~~~~
8+
!!! error TS2661: Cannot export 'string'. Only local declarations can be exported from a module.
9+
export type { number };
10+
~~~~~~
11+
!!! error TS2661: Cannot export 'number'. Only local declarations can be exported from a module.
12+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [exportNonLocalDeclarations.ts]
2+
export { string };
3+
export type { number };
4+
5+
6+
//// [exportNonLocalDeclarations.js]
7+
"use strict";
8+
exports.__esModule = true;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/externalModules/exportNonLocalDeclarations.ts ===
2+
export { string };
3+
>string : Symbol(string, Decl(exportNonLocalDeclarations.ts, 0, 8))
4+
5+
export type { number };
6+
>number : Symbol(number, Decl(exportNonLocalDeclarations.ts, 1, 13))
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/externalModules/exportNonLocalDeclarations.ts ===
2+
export { string };
3+
>string : any
4+
5+
export type { number };
6+
>number : any
7+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { string };
2+
export type { number };

0 commit comments

Comments
 (0)