Skip to content

Commit 18b8625

Browse files
vvakamesandersn
authored andcommitted
fix error on globalThis type extend (#30460)
* Add test for extend globalThis * Fix compile aborting
1 parent d0646a6 commit 18b8625

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27980,8 +27980,8 @@ namespace ts {
2798027980

2798127981
// The following checks only apply on a non-ambient instantiated module declaration.
2798227982
if (symbol.flags & SymbolFlags.ValueModule
27983-
&& symbol.declarations.length > 1
2798427983
&& !inAmbientContext
27984+
&& symbol.declarations.length > 1
2798527985
&& isInstantiatedModule(node, !!compilerOptions.preserveConstEnums || !!compilerOptions.isolatedModules)) {
2798627986
const firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol);
2798727987
if (firstNonAmbientClassOrFunc) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/extendGlobalThis.ts] ////
2+
3+
//// [extension.d.ts]
4+
declare global {
5+
namespace globalThis {
6+
var test: string;
7+
}
8+
}
9+
10+
export {}
11+
12+
//// [index.ts]
13+
import "./extention";
14+
15+
globalThis.tests = "a-b";
16+
console.log(globalThis.test.split("-"));
17+
18+
19+
//// [index.js]
20+
"use strict";
21+
exports.__esModule = true;
22+
require("./extention");
23+
globalThis.tests = "a-b";
24+
console.log(globalThis.test.split("-"));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/extension.d.ts ===
2+
declare global {
3+
>global : Symbol(global, Decl(extension.d.ts, 0, 0))
4+
5+
namespace globalThis {
6+
>globalThis : Symbol(globalThis)
7+
8+
var test: string;
9+
>test : Symbol(test, Decl(extension.d.ts, 2, 11))
10+
}
11+
}
12+
13+
export {}
14+
15+
=== tests/cases/compiler/index.ts ===
16+
import "./extention";
17+
18+
globalThis.tests = "a-b";
19+
>globalThis : Symbol(globalThis)
20+
21+
console.log(globalThis.test.split("-"));
22+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
23+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
24+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
25+
>globalThis.test.split : Symbol(String.split, Decl(lib.es5.d.ts, --, --))
26+
>globalThis.test : Symbol(test, Decl(extension.d.ts, 2, 11))
27+
>globalThis : Symbol(globalThis)
28+
>test : Symbol(test, Decl(extension.d.ts, 2, 11))
29+
>split : Symbol(String.split, Decl(lib.es5.d.ts, --, --))
30+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/compiler/extension.d.ts ===
2+
declare global {
3+
>global : typeof global
4+
5+
namespace globalThis {
6+
>globalThis : typeof globalThis
7+
8+
var test: string;
9+
>test : string
10+
}
11+
}
12+
13+
export {}
14+
15+
=== tests/cases/compiler/index.ts ===
16+
import "./extention";
17+
18+
globalThis.tests = "a-b";
19+
>globalThis.tests = "a-b" : "a-b"
20+
>globalThis.tests : any
21+
>globalThis : typeof globalThis
22+
>tests : any
23+
>"a-b" : "a-b"
24+
25+
console.log(globalThis.test.split("-"));
26+
>console.log(globalThis.test.split("-")) : void
27+
>console.log : (message?: any, ...optionalParams: any[]) => void
28+
>console : Console
29+
>log : (message?: any, ...optionalParams: any[]) => void
30+
>globalThis.test.split("-") : string[]
31+
>globalThis.test.split : (separator: string | RegExp, limit?: number) => string[]
32+
>globalThis.test : string
33+
>globalThis : typeof globalThis
34+
>test : string
35+
>split : (separator: string | RegExp, limit?: number) => string[]
36+
>"-" : "-"
37+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @filename: extension.d.ts
2+
declare global {
3+
namespace globalThis {
4+
var test: string;
5+
}
6+
}
7+
8+
export {}
9+
10+
// @filename: index.ts
11+
import "./extention";
12+
13+
globalThis.tests = "a-b";
14+
console.log(globalThis.test.split("-"));

0 commit comments

Comments
 (0)