-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Skip ambient modules in globalThis #48938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11507,7 +11507,7 @@ namespace ts { | |
if (symbol === globalThisSymbol) { | ||
const varsOnly = new Map<string, Symbol>() as SymbolTable; | ||
members.forEach(p => { | ||
if (!(p.flags & SymbolFlags.BlockScoped)) { | ||
if (!(p.flags & SymbolFlags.BlockScoped) && !(p.flags & SymbolFlags.ValueModule && p.declarations?.length && every(p.declarations, isAmbientModule))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you make a global named Edit: Ah, you give an example above. I guess |
||
varsOnly.set(p.escapedName, p); | ||
} | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
tests/cases/conformance/es2019/globalThisAmbientModules.ts(8,39): error TS2339: Property '"ambientModule"' does not exist on type 'typeof globalThis'. | ||
tests/cases/conformance/es2019/globalThisAmbientModules.ts(11,33): error TS2339: Property '"ambientModule"' does not exist on type 'typeof globalThis'. | ||
|
||
|
||
==== tests/cases/conformance/es2019/globalThisAmbientModules.ts (2 errors) ==== | ||
declare module "ambientModule" { | ||
export type typ = 1 | ||
export var val: typ | ||
} | ||
namespace valueModule { export var val = 1 } | ||
namespace namespaceModule { export type typ = 1 } | ||
// should error | ||
type GlobalBad1 = (typeof globalThis)["\"ambientModule\""] | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2339: Property '"ambientModule"' does not exist on type 'typeof globalThis'. | ||
type GlobalOk1 = (typeof globalThis)["valueModule"] | ||
type GlobalOk2 = globalThis.namespaceModule.typ | ||
const bad1: (typeof globalThis)["\"ambientModule\""] = 'ambientModule' | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2339: Property '"ambientModule"' does not exist on type 'typeof globalThis'. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//// [globalThisAmbientModules.ts] | ||
declare module "ambientModule" { | ||
export type typ = 1 | ||
export var val: typ | ||
} | ||
namespace valueModule { export var val = 1 } | ||
namespace namespaceModule { export type typ = 1 } | ||
// should error | ||
type GlobalBad1 = (typeof globalThis)["\"ambientModule\""] | ||
type GlobalOk1 = (typeof globalThis)["valueModule"] | ||
type GlobalOk2 = globalThis.namespaceModule.typ | ||
const bad1: (typeof globalThis)["\"ambientModule\""] = 'ambientModule' | ||
|
||
|
||
//// [globalThisAmbientModules.js] | ||
var valueModule; | ||
(function (valueModule) { | ||
valueModule.val = 1; | ||
})(valueModule || (valueModule = {})); | ||
var bad1 = 'ambientModule'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
=== tests/cases/conformance/es2019/globalThisAmbientModules.ts === | ||
declare module "ambientModule" { | ||
>"ambientModule" : Symbol("ambientModule", Decl(globalThisAmbientModules.ts, 0, 0)) | ||
|
||
export type typ = 1 | ||
>typ : Symbol(typ, Decl(globalThisAmbientModules.ts, 0, 32)) | ||
|
||
export var val: typ | ||
>val : Symbol(val, Decl(globalThisAmbientModules.ts, 2, 14)) | ||
>typ : Symbol(typ, Decl(globalThisAmbientModules.ts, 0, 32)) | ||
} | ||
namespace valueModule { export var val = 1 } | ||
>valueModule : Symbol(valueModule, Decl(globalThisAmbientModules.ts, 3, 1)) | ||
>val : Symbol(val, Decl(globalThisAmbientModules.ts, 4, 34)) | ||
|
||
namespace namespaceModule { export type typ = 1 } | ||
>namespaceModule : Symbol(namespaceModule, Decl(globalThisAmbientModules.ts, 4, 44)) | ||
>typ : Symbol(typ, Decl(globalThisAmbientModules.ts, 5, 27)) | ||
|
||
// should error | ||
type GlobalBad1 = (typeof globalThis)["\"ambientModule\""] | ||
>GlobalBad1 : Symbol(GlobalBad1, Decl(globalThisAmbientModules.ts, 5, 49)) | ||
>globalThis : Symbol(globalThis) | ||
|
||
type GlobalOk1 = (typeof globalThis)["valueModule"] | ||
>GlobalOk1 : Symbol(GlobalOk1, Decl(globalThisAmbientModules.ts, 7, 58)) | ||
>globalThis : Symbol(globalThis) | ||
|
||
type GlobalOk2 = globalThis.namespaceModule.typ | ||
>GlobalOk2 : Symbol(GlobalOk2, Decl(globalThisAmbientModules.ts, 8, 51)) | ||
>globalThis : Symbol(globalThis) | ||
>namespaceModule : Symbol(namespaceModule, Decl(globalThisAmbientModules.ts, 4, 44)) | ||
>typ : Symbol(namespaceModule.typ, Decl(globalThisAmbientModules.ts, 5, 27)) | ||
|
||
const bad1: (typeof globalThis)["\"ambientModule\""] = 'ambientModule' | ||
>bad1 : Symbol(bad1, Decl(globalThisAmbientModules.ts, 10, 5)) | ||
>globalThis : Symbol(globalThis) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
=== tests/cases/conformance/es2019/globalThisAmbientModules.ts === | ||
declare module "ambientModule" { | ||
>"ambientModule" : typeof import("ambientModule") | ||
|
||
export type typ = 1 | ||
>typ : 1 | ||
|
||
export var val: typ | ||
>val : 1 | ||
} | ||
namespace valueModule { export var val = 1 } | ||
>valueModule : typeof valueModule | ||
>val : number | ||
>1 : 1 | ||
|
||
namespace namespaceModule { export type typ = 1 } | ||
>typ : 1 | ||
|
||
// should error | ||
type GlobalBad1 = (typeof globalThis)["\"ambientModule\""] | ||
>GlobalBad1 : any | ||
>globalThis : typeof globalThis | ||
|
||
type GlobalOk1 = (typeof globalThis)["valueModule"] | ||
>GlobalOk1 : typeof valueModule | ||
>globalThis : typeof globalThis | ||
|
||
type GlobalOk2 = globalThis.namespaceModule.typ | ||
>GlobalOk2 : 1 | ||
>globalThis : any | ||
>namespaceModule : any | ||
|
||
const bad1: (typeof globalThis)["\"ambientModule\""] = 'ambientModule' | ||
>bad1 : any | ||
>globalThis : typeof globalThis | ||
>'ambientModule' : "ambientModule" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
declare module "ambientModule" { | ||
export type typ = 1 | ||
export var val: typ | ||
} | ||
namespace valueModule { export var val = 1 } | ||
namespace namespaceModule { export type typ = 1 } | ||
// should error | ||
type GlobalBad1 = (typeof globalThis)["\"ambientModule\""] | ||
type GlobalOk1 = (typeof globalThis)["valueModule"] | ||
type GlobalOk2 = globalThis.namespaceModule.typ | ||
const bad1: (typeof globalThis)["\"ambientModule\""] = 'ambientModule' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some questions:
ValueModule
symbol flag/what is a value module?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
declare module "foo"
with values inside it. A namespace module is a namespace that has only types inside it. (Both kinds of module can contain other namespaces; any nested value turns the parent namespaces into value modules.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, the excludes are the same as other value modules:
Value & ~(Function | Class | RegularEnum | ValueModule)
I'll see if I can name a function, class or enum
"foo"
, but I don't think I can.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, effectively only other ambient modules can merge with ambient modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can if you use a js merge, eg,
or similar.