question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exported ambient variables are neither emitted nor an error

See original GitHub issue

TypeScript 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:open
  • Created 7 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
mhegazycommented, Apr 14, 2016

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

declare var foo: Foo;
export { foo };

should emit

exports.foo = foo; 

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:

var globalWindow = window;
export {globalWindow as window};
1reaction
Arnavioncommented, Apr 12, 2016

@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:

export type Foo = { bar: string; }
declare var foo: Foo;
var globalFoo = foo; // local binding to global
export { globalFoo as foo };
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found