-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Clarify what "ambient" means #180
Comments
We prefer 'global' and 'module' to distinguish these cases. Sometimes we say 'script' and 'external module' as well. None of them are without other connotations, unfortunately. 'ambient' means "without implementation". It's a confusing term to use the scope of .d.ts files because all declarations in a .d.ts file are implicitly ambient. |
"module"A module is a file with a top level "script"A script is a file that is not a "module". "ambient"Anything that does not have an implementation, e.g.:
A module can be ambient as well; so
|
Ok, thanks. Perhaps I'll need to change this to |
About "script", will it always contribute to the "global" namespace?
Sorry I'm not too good at reading specs. If that is the case, what should it be called in the context of ".d.ts": "ambient declaration", "ambient script declaration", or "ambient script"? UPDATE: An even harder question: what should it call if a module pollutes global namespace (such as jQuery)? Should |
module vs. script is about the file. i.e. does it have an import/export. |
IMO that's the confusing part. Consider the following, which are modules and which are scripts? // a.d.ts
import dr from 'domready';
// b.d.ts
declare function domready(): void;
export = domready;
// x.d.ts
declare module '~domready' {
function domready(): void;
}
declare module 'domready' {
import main = require('~domready');
export = main;
}
// y.d.ts
declare namespace Y {
export function foo() { ... }
} If you mean top-level import/export, then "x.d.ts" and "y.d.ts" are script (while "x.d.ts" is a module), Another difficulty is to have different terms for "b.d.ts" and "x.d.ts".
|
and
If module vs script is about the file, then may be |
A script file dose not introduce a new scope; all its top level declarations live in the global scope. script file pollute the global namespace. you can, and should only have one of these per compilation per framework. A module introduces its own scope, it provides isolation, and you can have multiple of them with no conflicts. modules should be the way to go, unless you are really putting stuff in the global namespace, and you understand what that entails. This is the approach we tool with the changes in UMD work (see microsoft/TypeScript#7264). so something like jquery should be defined as a module with a UMD global export for |
While writing @typings I had to come up with a name for those non-external module definitions. I settled on ambient because it seemed intuitive, the TypeScript compiler would use it in error messages and all other "global"-like names are even more overloaded. However, there's one sentence with this interpretation that doesn't work in the handbook.
So technically, does that mean even external module definitions are considered "ambient". Is there a better term we should adopt to explain external and non-external module definitions? Preferably something with less syllables and one word that can be used in CLI scripts.
The text was updated successfully, but these errors were encountered: