Skip to content

Commit 29b9fd3

Browse files
committed
Merge pull request microsoft#7119 from Microsoft/checkRedeclarations
correctly check exported type aliases merged with overloads
2 parents fdb8dc8 + 5e770bd commit 29b9fd3

5 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/compiler/checker.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15055,11 +15055,20 @@ namespace ts {
1505515055
continue;
1505615056
}
1505715057
const { declarations, flags } = exports[id];
15058-
// ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. (TS Exceptions: namespaces, function overloads, enums, and interfaces)
15059-
if (!(flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) && (flags & SymbolFlags.TypeAlias ? declarations.length - 1 : declarations.length) > 1) {
15060-
const exportedDeclarations: Declaration[] = filter(declarations, isNotOverload);
15061-
if (exportedDeclarations.length > 1) {
15062-
for (const declaration of exportedDeclarations) {
15058+
// ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries.
15059+
// (TS Exceptions: namespaces, function overloads, enums, and interfaces)
15060+
if (flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) {
15061+
continue;
15062+
}
15063+
const exportedDeclarationsCount = countWhere(declarations, isNotOverload);
15064+
if (flags & SymbolFlags.TypeAlias && exportedDeclarationsCount <= 2) {
15065+
// it is legal to merge type alias with other values
15066+
// so count should be either 1 (just type alias) or 2 (type alias + merged value)
15067+
continue;
15068+
}
15069+
if (exportedDeclarationsCount > 1) {
15070+
for (const declaration of declarations) {
15071+
if (isNotOverload(declaration)) {
1506315072
diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Cannot_redeclare_exported_variable_0, id));
1506415073
}
1506515074
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [exportRedeclarationTypeAliases.ts]
2+
export type Foo = number;
3+
export function Foo(): number;
4+
export function Foo(): any {}
5+
6+
//// [exportRedeclarationTypeAliases.js]
7+
"use strict";
8+
function Foo() { }
9+
exports.Foo = Foo;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/exportRedeclarationTypeAliases.ts ===
2+
export type Foo = number;
3+
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
4+
5+
export function Foo(): number;
6+
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
7+
8+
export function Foo(): any {}
9+
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/exportRedeclarationTypeAliases.ts ===
2+
export type Foo = number;
3+
>Foo : number
4+
5+
export function Foo(): number;
6+
>Foo : () => number
7+
8+
export function Foo(): any {}
9+
>Foo : () => number
10+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @module: commonjs
2+
export type Foo = number;
3+
export function Foo(): number;
4+
export function Foo(): any {}

0 commit comments

Comments
 (0)