Description
When a type declaration in an inner module has the same name as a type declaration in an outer module, the outer declaration becomes inaccessible. Programmers can typically work around this, for example by deriving a type with an alternate name in the outer module or, with our latest changes, by declaring a type alias. However, such manual work-arounds are not a good option for code generation tools such as the .d.ts generation facility provided by the compiler. We really need a fool proof way to ensure a lookup starts at the global scope. Consider:
function makeError(): Error {
return new Error("We've got a problem");
}
module M {
export class Error {
}
export var x = makeError(); // No way to name global Error type here
}
It is currently impossible to create a .d.ts file for the code above because there is no way to emit a type annotation for x
that references the global interface Error
.
Node.js uses the name global
for the global scope. We could consider supporting something similar in type names. Specifically, if a qualified type name starts with the identifier global
and if a lookup of global
as a module name doesn't bind to anything (meaning the user didn't define a global
module), then we consider it a globally qualified name. For the example above we could then generate the following .d.ts file:
declare module M {
class Error {
}
var x: global.Error;
}
If course, if the user defines their own global
module we now have a problem again--but I think it is fine to just say you shouldn't do that. We could even consider making it an error, or allowing it only under a compiler switch.
Activity
mhegazy commentedon Oct 31, 2014
how about a global modifier instead of reserving the name something like ``global::`:
DanielRosenwasser commentedon Nov 1, 2014
global::
is interesting. C++ has::x
to retrieve anx
in the global namespace, so it sort of reminds me of that.RyanCavanaugh commentedon Nov 3, 2014
We just as much need to solve the problem for external modules, so we'd need another special identifier that meant "the containing external module".
global
makes sense for internal modules but I think we'll have a hard time finding a non-conflicting equivalent for external modules. Just using a different syntax seems like a cleaner solution at that point.sccolbert commentedon Nov 20, 2014
👍 for a way to explicitly resolve global scope
basarat commentedon Dec 11, 2014
It is possible with a bit of _ab_use of
declare var
andtypeof
:Related stackoverflow question: http://stackoverflow.com/a/27433864/390330
ghost commentedon Jan 12, 2015
I think the fact that these kinds of ugly workarounds are necessary illustrates the need for a supported method of accessing the global scope.
I could get behind either
global::foo
or::foo
.basarat commentedon Jan 13, 2015
👍 for
::foo
Reason: I don't like the confusion
global
will create with respect to something actually calledglobal
e.g. http://nodejs.org/api/globals.html#globals_globalmhegazy commentedon Jan 14, 2015
The main objection to :: is that it looks bad in a type position:
var x: ::Error;
cseufert commentedon Feb 16, 2015
Could we just use a leading . so .File or .Error, in the vein of PHP with \BuiltInClass.
Alternatively, when in a browser context, use window.File, or window.Error, since whats were they are declared, rather than adding a new syntax.
41 remaining items
bartzy commentedon Mar 2, 2016
Thanks, it works!
I was under the impression that
const localTouchEvent = TouchEvent
does capture the type as well.I also didn't get what syntax is
import localTouchEvent = TouchEvent
? It's not ES6 syntax, right?mhegazy commentedon Mar 2, 2016
no, but types are not an
ES6
either.global
as a type #14052rjamesnw commentedon Apr 5, 2017
Just checking - is this still an issue, or is there a solution to this yet? I used to have issues access types using some kind of fully qualified naming approach (as Anders suggested), but not sure where things stand now after much time has passed.
mnpenner commentedon Aug 3, 2017
Yeah, where are we at with this? Is there a way to reference a global that collides with something inside the namespace or no?
fly-studio commentedon Dec 6, 2017
As this problem
I known how to close this error, like this, but it'll export a global variant
euiIAssetAdapter
Do this have a root namespace symbol, like PHP
\eui\IAssetAdapter
or::eui.IAssetAdapter
Fix type declarations inadvertedtly referencing Polymer.Element.
Fix type declarations inadvertedtly referencing Polymer.Element.
Fix type declarations inadvertedtly referencing Polymer.Element. (#5084)
inad9300 commentedon May 12, 2018
Any progress on this front?