-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Lib files leak "support" types into global namespace #15529
Comments
The let x: ObjectConstructor; // Ok, referenced as type name
let y = ObjectConstructor; // Error, can't use a type name as a value Declarations often introduce names in both namespaces. For example, a class declaration introduces a name in the value namespace (the constructor function) and the type namespace (the class instance type). This provides the illusion of a connection between the two, but they're really completely separate. |
I'm still learning my way around declaration files, but wouldn't it be possible to put the Something like: In lib.es5.support.d.ts: declare interface ObjectConstructor {
new (value?: any): Object;
(): any;
(value: any): any;
// ...
}
export declare const Object: ObjectConstructor; and in lib.es5.d.ts: import { Object } from './lib.es5.support';
interface Object {
constructor: Function;
// ...
} Something similar to this appears to work with a simple local test file (admittedly a declaration file and a TypeScript file, not declaration→declaration→JavaScript... though it seems portable)... |
Not at the moment, because as soon as you include an import inside of a file, it becomes a module. That means That said, we could add support for referencing types within a global context (@yuit was looking into this), or we could even put all of these types within a global namespace called I do share your concern. As the person who introduced |
Yes, understood.
@ahejlsberg This I don't understand. When using the latest VSCode, I can type As a user, I expect that including the ECMAScript spec in a project ( |
@ericdrobinson, @mhegazy can better answer, but we introduce type-only suggestions to not be overly-strict. A lot of the time you'll end up in a state where it makes no sense semantically to suggest a type - however, the reality of it is that people write code in weird ways to take shortcuts (including myself). We could be more rigid about this. |
@DanielRosenwasser I guess this speaks to one of the main reasons that I am so attracted to TypeScript as a language. When using it alongside JavaScript (or, more accurately, an ECMAScript-based language) it can significantly improve usability and discoverability of the language features. This (discoverability, especially) becomes incredibly muddied when you litter the global namespace with types like this. |
@DanielRosenwasser When you say "be more rigid", do you mean on the IntelliSense side? The declaration implementation side? Or the TypeScript language feature side? I don't have a problem with the flexibility. What's confusing, especially as someone new to the language, is why all these unexpected types show up. If you've only had JavaScript experience you may start poking around for Personally, I didn't actually understand what |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
@mhegazy As this has issue has been closed because it was labeled as "Working as Intended", what is the appropriate way to issue a feature request? Specifically, as a user, I'd prefer to have an option that would allow me to set up my environment in such a way that, by default, it only fills the type and value namespaces with content as outlined in the various specs associated with the libraries specified using the This will improve target language API discoverability and usability as listed types would theoretically have some form of useful results when google'd. This would also free up folks like @DanielRosenwasser to create new helper types like In short, I cannot think of a time that I would actually care about So... feature requests? Should I open a new ticket or is there another way to handle this? |
For auto complete clutter, #15750 tracks filtering it.
the library file is not special, it is a rather normal .d.ts file. just create your own lib.d.ts file, and include it in your complication with |
Autocompletion ClutterThanks, @mhegazy, for the replies!
Your clarification appears to imply that the issue I'm attempting to raise here is not covered by #15750:
Addressing this issue would not stop me from seeing Global Declaration Library Files that Keep Implementation Details Private
I think you may have misunderstood my request. I understand that the library files are not special - they are "pre-built" global declaration files that attempt to describe the associated ECMAScript spec). The problem is that adding them brings in a whole bunch of mostly useless helper-types - types that typically come with no documentation as they are generally intended as declaration-"internal". As a concrete example, if I specify Put another way: I'd like some way to mark a type as an "implementation detail" and not something that should bleed into the global type/value contexts. I think @DanielRosenwasser's comment was starting to get at this. |
TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)
Description
Core ECMAScript lib declaration files (e.g. lib.es5.d.ts) leak "internal" interface types into the global namespace. A common pattern employed in the lib files is that "class"-static features are placed in a
[TypeName]Constructor
interface and then aconst
variable of that type is declared with the same TypeName as the[TypeName]
interface. Example:The problem is that the
ObjectConstructor
type is leaked into the global namespace. No such type is defined in the ECMAScript 5.1 language spec, and yet it clutters up auto-complete suggestions.The
[TypeName]Constructor
pattern seems like a workaround to enable certain features to be expressed in the lib files. The leaking of these types seems to be a side-effect of this construction.Expected Behavior
Including a core ECMAScript lib file into a project (whether by default or compilerOption) only adds types found in the associated ECMAScript specifications into the global namespace.
Actual Behavior
[TypeName]Constructor
types end up in the global namespace for any project that includes a library utilizing such a pattern.The text was updated successfully, but these errors were encountered: