Skip to content

Commit 5762519

Browse files
committed
Resolve aliases to preserve 'Cannot find name' errors for namespace imports
1 parent fde0a28 commit 5762519

File tree

5 files changed

+99
-6
lines changed

5 files changed

+99
-6
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -999,12 +999,19 @@ namespace ts {
999999
}
10001000

10011001
const nameAsType = resolveName(errorLocation, name, strictlyTypeMeanings, nameNotFoundMessage, nameArg);
1002-
if (nameAsType) {
1003-
error(errorLocation, Diagnostics.Cannot_find_name_0_A_type_exists_with_this_name_but_no_value, name);
1004-
return true;
1002+
if (!nameAsType) {
1003+
return false;
10051004
}
10061005

1007-
return false;
1006+
if (nameAsType.flags & SymbolFlags.Alias) {
1007+
const resolvedSymbol = resolveAlias(nameAsType);
1008+
if (resolvedSymbol.flags & SymbolFlags.NamespaceModule) {
1009+
return false;
1010+
}
1011+
}
1012+
1013+
error(errorLocation, Diagnostics.Cannot_find_name_0_A_type_exists_with_this_name_but_no_value, name);
1014+
return true;
10081015
}
10091016

10101017
function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void {

tests/baselines/reference/aliasOnMergedModuleInterface.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/aliasOnMergedModuleInterface_1.ts(5,16): error TS2691: Cannot find name 'foo'. A type exists with this name, but no value.
1+
tests/cases/compiler/aliasOnMergedModuleInterface_1.ts(5,16): error TS2304: Cannot find name 'foo'.
22

33

44
==== tests/cases/compiler/aliasOnMergedModuleInterface_1.ts (1 errors) ====
@@ -8,7 +8,7 @@ tests/cases/compiler/aliasOnMergedModuleInterface_1.ts(5,16): error TS2691: Cann
88
z.bar("hello"); // This should be ok
99
var x: foo.A = foo.bar("hello"); // foo.A should be ok but foo.bar should be error
1010
~~~
11-
!!! error TS2691: Cannot find name 'foo'. A type exists with this name, but no value.
11+
!!! error TS2304: Cannot find name 'foo'.
1212

1313
==== tests/cases/compiler/aliasOnMergedModuleInterface_0.ts (0 errors) ====
1414
declare module "foo"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
tests/cases/compiler/world.ts(4,1): error TS2691: Cannot find name 'HelloInterface'. A type exists with this name, but no value.
2+
tests/cases/compiler/world.ts(5,1): error TS2304: Cannot find name 'HelloNamespace'.
3+
4+
5+
==== tests/cases/compiler/world.ts (2 errors) ====
6+
import HelloInterface = require("helloInterface");
7+
import HelloNamespace = require("helloNamespace");
8+
9+
HelloInterface.world;
10+
~~~~~~~~~~~~~~
11+
!!! error TS2691: Cannot find name 'HelloInterface'. A type exists with this name, but no value.
12+
HelloNamespace.world;
13+
~~~~~~~~~~~~~~
14+
!!! error TS2304: Cannot find name 'HelloNamespace'.
15+
==== tests/cases/compiler/helloInterface.ts (0 errors) ====
16+
interface HelloInterface {
17+
world: any;
18+
}
19+
20+
export = HelloInterface;
21+
22+
==== tests/cases/compiler/helloNamespace.ts (0 errors) ====
23+
namespace HelloNamespace {
24+
export type world = any;
25+
}
26+
27+
export = HelloNamespace;
28+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [tests/cases/compiler/typeUsedAsValueError2.ts] ////
2+
3+
//// [helloInterface.ts]
4+
interface HelloInterface {
5+
world: any;
6+
}
7+
8+
export = HelloInterface;
9+
10+
//// [helloNamespace.ts]
11+
namespace HelloNamespace {
12+
export type world = any;
13+
}
14+
15+
export = HelloNamespace;
16+
17+
//// [world.ts]
18+
import HelloInterface = require("helloInterface");
19+
import HelloNamespace = require("helloNamespace");
20+
21+
HelloInterface.world;
22+
HelloNamespace.world;
23+
24+
//// [helloInterface.js]
25+
define(["require", "exports"], function (require, exports) {
26+
"use strict";
27+
});
28+
//// [helloNamespace.js]
29+
define(["require", "exports"], function (require, exports) {
30+
"use strict";
31+
});
32+
//// [world.js]
33+
define(["require", "exports"], function (require, exports) {
34+
"use strict";
35+
HelloInterface.world;
36+
HelloNamespace.world;
37+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @module: amd
2+
// @filename: helloInterface.ts
3+
interface HelloInterface {
4+
world: any;
5+
}
6+
7+
export = HelloInterface;
8+
9+
// @filename: helloNamespace.ts
10+
namespace HelloNamespace {
11+
export type world = any;
12+
}
13+
14+
export = HelloNamespace;
15+
16+
// @filename: world.ts
17+
import HelloInterface = require("helloInterface");
18+
import HelloNamespace = require("helloNamespace");
19+
20+
HelloInterface.world;
21+
HelloNamespace.world;

0 commit comments

Comments
 (0)