Skip to content

Commit 5d545aa

Browse files
Allow import = in module augmentations (microsoft#57704)
1 parent 6f64642 commit 5d545aa

9 files changed

+167
-38
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -47029,6 +47029,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4702947029
grammarErrorOnFirstToken(node, Diagnostics.Exports_and_export_assignments_are_not_permitted_in_module_augmentations);
4703047030
break;
4703147031
case SyntaxKind.ImportEqualsDeclaration:
47032+
// import a = e.x; in module augmentation is ok, but not import a = require('fs)
47033+
if (isInternalModuleImportEqualsDeclaration(node)) break;
47034+
// falls through
4703247035
case SyntaxKind.ImportDeclaration:
4703347036
grammarErrorOnFirstToken(node, Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module);
4703447037
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
importAliasInModuleAugmentation.ts(12,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
2+
importAliasInModuleAugmentation.ts(12,24): error TS2307: Cannot find module 'fs' or its corresponding type declarations.
3+
4+
5+
==== importAliasInModuleAugmentation.ts (2 errors) ====
6+
export { }
7+
8+
namespace A {
9+
export const y = 34;
10+
export interface y { s: string }
11+
}
12+
13+
declare global {
14+
export import x = A.y;
15+
16+
// Should still error
17+
import f = require("fs");
18+
~~~~~~
19+
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
20+
~~~~
21+
!!! error TS2307: Cannot find module 'fs' or its corresponding type declarations.
22+
}
23+
24+
const m: number = x;
25+
let s: x = { s: "" };
26+
void s.s;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/importAliasInModuleAugmentation.ts] ////
2+
3+
//// [importAliasInModuleAugmentation.ts]
4+
export { }
5+
6+
namespace A {
7+
export const y = 34;
8+
export interface y { s: string }
9+
}
10+
11+
declare global {
12+
export import x = A.y;
13+
14+
// Should still error
15+
import f = require("fs");
16+
}
17+
18+
const m: number = x;
19+
let s: x = { s: "" };
20+
void s.s;
21+
22+
//// [importAliasInModuleAugmentation.js]
23+
"use strict";
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
var A;
26+
(function (A) {
27+
A.y = 34;
28+
})(A || (A = {}));
29+
var m = x;
30+
var s = { s: "" };
31+
void s.s;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [tests/cases/compiler/importAliasInModuleAugmentation.ts] ////
2+
3+
=== importAliasInModuleAugmentation.ts ===
4+
export { }
5+
6+
namespace A {
7+
>A : Symbol(A, Decl(importAliasInModuleAugmentation.ts, 0, 10))
8+
9+
export const y = 34;
10+
>y : Symbol(y, Decl(importAliasInModuleAugmentation.ts, 3, 16), Decl(importAliasInModuleAugmentation.ts, 3, 24))
11+
12+
export interface y { s: string }
13+
>y : Symbol(y, Decl(importAliasInModuleAugmentation.ts, 3, 16), Decl(importAliasInModuleAugmentation.ts, 3, 24))
14+
>s : Symbol(y.s, Decl(importAliasInModuleAugmentation.ts, 4, 24))
15+
}
16+
17+
declare global {
18+
>global : Symbol(global, Decl(importAliasInModuleAugmentation.ts, 5, 1))
19+
20+
export import x = A.y;
21+
>x : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 7, 16))
22+
>A : Symbol(A, Decl(importAliasInModuleAugmentation.ts, 0, 10))
23+
>y : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 3, 16), Decl(importAliasInModuleAugmentation.ts, 3, 24))
24+
25+
// Should still error
26+
import f = require("fs");
27+
>f : Symbol(f, Decl(importAliasInModuleAugmentation.ts, 8, 26))
28+
}
29+
30+
const m: number = x;
31+
>m : Symbol(m, Decl(importAliasInModuleAugmentation.ts, 14, 5))
32+
>x : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 7, 16))
33+
34+
let s: x = { s: "" };
35+
>s : Symbol(s, Decl(importAliasInModuleAugmentation.ts, 15, 3))
36+
>x : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 7, 16))
37+
>s : Symbol(s, Decl(importAliasInModuleAugmentation.ts, 15, 12))
38+
39+
void s.s;
40+
>s.s : Symbol(x.s, Decl(importAliasInModuleAugmentation.ts, 4, 24))
41+
>s : Symbol(s, Decl(importAliasInModuleAugmentation.ts, 15, 3))
42+
>s : Symbol(x.s, Decl(importAliasInModuleAugmentation.ts, 4, 24))
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/importAliasInModuleAugmentation.ts] ////
2+
3+
=== importAliasInModuleAugmentation.ts ===
4+
export { }
5+
6+
namespace A {
7+
>A : typeof A
8+
9+
export const y = 34;
10+
>y : 34
11+
>34 : 34
12+
13+
export interface y { s: string }
14+
>s : string
15+
}
16+
17+
declare global {
18+
>global : typeof global
19+
20+
export import x = A.y;
21+
>x : 34
22+
>A : typeof A
23+
>y : x
24+
25+
// Should still error
26+
import f = require("fs");
27+
>f : any
28+
}
29+
30+
const m: number = x;
31+
>m : number
32+
>x : 34
33+
34+
let s: x = { s: "" };
35+
>s : x
36+
>{ s: "" } : { s: string; }
37+
>s : string
38+
>"" : ""
39+
40+
void s.s;
41+
>void s.s : undefined
42+
>s.s : string
43+
>s : x
44+
>s : string
45+

tests/baselines/reference/moduleAugmentationImportsAndExports2.errors.txt

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ f3.ts(11,5): error TS2667: Imports are not permitted in module augmentations. Co
33
f3.ts(11,21): error TS2307: Cannot find module './f2' or its corresponding type declarations.
44
f3.ts(12,5): error TS2666: Exports and export assignments are not permitted in module augmentations.
55
f3.ts(12,21): error TS2307: Cannot find module './f2' or its corresponding type declarations.
6-
f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
7-
f3.ts(14,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
86
f4.ts(5,11): error TS2339: Property 'foo' does not exist on type 'A'.
97

108

@@ -16,7 +14,7 @@ f4.ts(5,11): error TS2339: Property 'foo' does not exist on type 'A'.
1614
n: number;
1715
}
1816

19-
==== f3.ts (7 errors) ====
17+
==== f3.ts (5 errors) ====
2018
import {A} from "./f1";
2119

2220
A.prototype.foo = function () { return undefined; }
@@ -40,11 +38,7 @@ f4.ts(5,11): error TS2339: Property 'foo' does not exist on type 'A'.
4038
~~~~~~
4139
!!! error TS2307: Cannot find module './f2' or its corresponding type declarations.
4240
import I = N.Ifc;
43-
~~~~~~
44-
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
4541
import C = N.Cls;
46-
~~~~~~
47-
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
4842
// should have explicit export
4943
interface A {
5044
foo(): B;

tests/baselines/reference/moduleAugmentationImportsAndExports3.errors.txt

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
f3.ts(11,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
22
f3.ts(11,21): error TS2307: Cannot find module './f2' or its corresponding type declarations.
3-
f3.ts(12,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
4-
f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
53

64

75
==== f1.ts (0 errors) ====
@@ -12,7 +10,7 @@ f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Co
1210
n: number;
1311
}
1412

15-
==== f3.ts (4 errors) ====
13+
==== f3.ts (2 errors) ====
1614
import {A} from "./f1";
1715

1816
A.prototype.foo = function () { return undefined; }
@@ -29,11 +27,7 @@ f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Co
2927
~~~~~~
3028
!!! error TS2307: Cannot find module './f2' or its corresponding type declarations.
3129
import I = N.Ifc;
32-
~~~~~~
33-
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
3430
import C = N.Cls;
35-
~~~~~~
36-
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
3731
interface A {
3832
foo(): B;
3933
bar(): I;

tests/baselines/reference/newNamesInGlobalAugmentations1.errors.txt

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export { }
2+
3+
namespace A {
4+
export const y = 34;
5+
export interface y { s: string }
6+
}
7+
8+
declare global {
9+
export import x = A.y;
10+
11+
// Should still error
12+
import f = require("fs");
13+
}
14+
15+
const m: number = x;
16+
let s: x = { s: "" };
17+
void s.s;

0 commit comments

Comments
 (0)