mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 20:14:01 -06:00
correctly check exported type aliases merged with overloads
This commit is contained in:
parent
d92f78d7e2
commit
5e770bda2e
@ -15055,11 +15055,20 @@ namespace ts {
|
||||
continue;
|
||||
}
|
||||
const { declarations, flags } = exports[id];
|
||||
// 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)
|
||||
if (!(flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) && (flags & SymbolFlags.TypeAlias ? declarations.length - 1 : declarations.length) > 1) {
|
||||
const exportedDeclarations: Declaration[] = filter(declarations, isNotOverload);
|
||||
if (exportedDeclarations.length > 1) {
|
||||
for (const declaration of exportedDeclarations) {
|
||||
// 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)
|
||||
if (flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) {
|
||||
continue;
|
||||
}
|
||||
const exportedDeclarationsCount = countWhere(declarations, isNotOverload);
|
||||
if (flags & SymbolFlags.TypeAlias && exportedDeclarationsCount <= 2) {
|
||||
// it is legal to merge type alias with other values
|
||||
// so count should be either 1 (just type alias) or 2 (type alias + merged value)
|
||||
continue;
|
||||
}
|
||||
if (exportedDeclarationsCount > 1) {
|
||||
for (const declaration of declarations) {
|
||||
if (isNotOverload(declaration)) {
|
||||
diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Cannot_redeclare_exported_variable_0, id));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
//// [exportRedeclarationTypeAliases.ts]
|
||||
export type Foo = number;
|
||||
export function Foo(): number;
|
||||
export function Foo(): any {}
|
||||
|
||||
//// [exportRedeclarationTypeAliases.js]
|
||||
"use strict";
|
||||
function Foo() { }
|
||||
exports.Foo = Foo;
|
||||
@ -0,0 +1,10 @@
|
||||
=== tests/cases/compiler/exportRedeclarationTypeAliases.ts ===
|
||||
export type Foo = number;
|
||||
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
|
||||
|
||||
export function Foo(): number;
|
||||
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
|
||||
|
||||
export function Foo(): any {}
|
||||
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
=== tests/cases/compiler/exportRedeclarationTypeAliases.ts ===
|
||||
export type Foo = number;
|
||||
>Foo : number
|
||||
|
||||
export function Foo(): number;
|
||||
>Foo : () => number
|
||||
|
||||
export function Foo(): any {}
|
||||
>Foo : () => number
|
||||
|
||||
4
tests/cases/compiler/exportRedeclarationTypeAliases.ts
Normal file
4
tests/cases/compiler/exportRedeclarationTypeAliases.ts
Normal file
@ -0,0 +1,4 @@
|
||||
// @module: commonjs
|
||||
export type Foo = number;
|
||||
export function Foo(): number;
|
||||
export function Foo(): any {}
|
||||
Loading…
x
Reference in New Issue
Block a user