Exported ambient variables are neither emitted nor an error
See original GitHub issueTypeScript Version:
1.9.0-dev.20160411
Code
foo.ts:
export type Foo = { bar: string; }
declare var foo: Foo;
export { foo };
bar.ts:
import { foo } from "./foo";
console.log(foo.bar);
tsc -t es5 -m commonjs ./foo.ts ./bar.ts
Expected behavior:
Either it should be an error to export an ambient variable, or it should be emitted something like this:
foo.js:
"use strict";
exports.foo = foo;
bar.js:
"use strict";
var foo_1 = require("./foo");
console.log(foo_1.foo.bar);
Actual behavior:
No error, and foo.js is emitted with no export, causing bar.js to blow up at runtime.
foo.js:
"use strict";
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (9 by maintainers)
Top Results From Across the Web
import is not working after exporting an environment variable ...
But still import statement is not working. It is throwing error saying that "ImportError: No module named phx_commonlib.configuration.
Read more >Where variables can be used - GitLab Documentation
As it's described in the CI/CD variables documentation, you can define many different variables. Some of them can be used for all GitLab...
Read more >Error with environment variable expansion in ZSH
As you can see, the variable substitution does not do what we expected. What is the issue here? shell · zsh · environment-variables...
Read more >Output - webpack
Otherwise, loading the resulting AMD bundle directly will result in an error like define is not defined . With the following configuration... module.exports...
Read more >Set Connection References and Environment Variables in ...
When you import a solution that contains either Connection References you don't have associated with a connection or Environment Variables ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just to clarify:
Exporting a name that was not declared in the module is an error as per the ES6 spec. the compiler flags this correctly today. see https://github.com/Microsoft/TypeScript/issues/6323#issuecomment-170170230
Exporting a name that is declared in the module is legal, regardless if it is ambient or not, the emitter should emit it correctly, and this issue is tracking fixing this. so
should emit
Now, one thing to note, if you are doing this to capture a global variable, which it seems @Arnavion and @ddotlic are doing, then you need to be aware that this is going to be an error if/when your run this code under a native ES6 module implementation. the fact that the compiler does not flag it does not make it ok at runtime. so consider declaring a local name:
@mhegazy Yeah, I did think
export
would only allow local bindings, so yes TS should make that an error.The intent was to use an external global and annotate it with a local type, so the workaround I went with was: