Skip to content
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

dynamically imported module without import / export is treated as script by tsc #9593

Closed
kt3k opened this issue Feb 24, 2021 · 6 comments · Fixed by #14875
Closed

dynamically imported module without import / export is treated as script by tsc #9593

kt3k opened this issue Feb 24, 2021 · 6 comments · Fixed by #14875
Labels
bug Something isn't working correctly needs discussion this topic needs further discussion to determine what action to take upstream Changes in upstream are required to solve these issues

Comments

@kt3k
Copy link
Member

kt3k commented Feb 24, 2021

(Note: This probably doesn't affect any meaningful use cases, but I create issue for record.)

When we have the following scripts:

a.ts

const a = 1;
await import("./b.ts");

b.ts

const a = 2;

Then execute a.ts and it causes the following error.

$ deno run --allow-read a.ts
error: TS2451 [ERROR]: Cannot redeclare block-scoped variable 'a'.
const a = 2;
      ^
    at file:///Users/kt3k/0/di-test/b.ts:1:7

    'a' was also declared here.
    const a = 1;
          ^
        at file:///Users/kt3k/0/di-test/a.ts:1:7

This doesn't happen when:

  • b.ts has any import or export statement
  • import of b.ts is not dynamic import

$ deno --version
deno 1.7.5 (release, x86_64-apple-darwin)
v8 9.0.123
typescript 4.1.4
@bartlomieju
Copy link
Member

bartlomieju commented Feb 24, 2021

For the record this is TSC error and not error coming from V8.

EDIT: This error doesn't happen if files are .js. Luca suggests that it is TSC that treats dynamically imported file as a script not a module.

@bartlomieju bartlomieju added bug Something isn't working correctly needs discussion this topic needs further discussion to determine what action to take labels Feb 24, 2021
@nayeemrmn
Copy link
Collaborator

This is an example of where simply ignoring TS2306:

// TS2306: File 'file:///Users/rld/src/deno/cli/tests/subdir/amd_like.js' is
// not a module.
2306,
is not enough, and we need a real way of telling the TypeScript compiler out-of-band that all source files should be treated as ES modules. Unfortunately no such compiler option is provided by TypeScript, so we tell users to work around this by adding export {}; in the imported module.

@nayeemrmn
Copy link
Collaborator

Ref microsoft/TypeScript#18232.

@kitsonk kitsonk changed the title Scopes are merged when dynamically imported module doesn't have any import / export dynamically imported module withoit import / export is treated as script by tsc Feb 24, 2021
@kitsonk
Copy link
Contributor

kitsonk commented Feb 24, 2021

That old problem. There is no easy way around it except export {}, but is dynamically importing a module that doesn't export anything actually a common realistic problem?

I suggest we simply add a note to the docs FAQs about this know TypeScript issue and be done with it.

@kitsonk kitsonk changed the title dynamically imported module withoit import / export is treated as script by tsc dynamically imported module without import / export is treated as script by tsc Feb 25, 2021
@nayeemrmn
Copy link
Collaborator

@kitsonk
Copy link
Contributor

kitsonk commented Oct 3, 2021

As stated in the response, the option doesn't fix the issue and can't fix the issue as tsc is currently artchitected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly needs discussion this topic needs further discussion to determine what action to take upstream Changes in upstream are required to solve these issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants