The Wayback Machine - https://web.archive.org/web/20211015153718/https://github.com/AssemblyScript/assemblyscript/issues/2066
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

portability: allow .js in import specifiers #2066

Open
trusktr opened this issue Sep 11, 2021 · 1 comment
Open

portability: allow .js in import specifiers #2066

trusktr opened this issue Sep 11, 2021 · 1 comment

Comments

@trusktr
Copy link
Member

@trusktr trusktr commented Sep 11, 2021

It is awkward, but TypeScript officially supports using .js in import specifiers, like so:

import {foo} from './path/to/some/file.js'
console.log(foo)

where ./path/to/some/file.js does not actually exist, but the file ./path/to/some/file.ts actually exists, and TypeScript will correctly (officially) use file.ts.

In fact, to show how official it is, TypeScript more recently even added the feature that when you auto-import something, if you are already use .js extensions in all you import specifiers, the new automatically-added import statement will also have the .js extension for consistency.

But why?

When compiling TypeScript code to JavaScript, we want it to work out of the box in native ES Module systems (like in browsers).

TypeScript team has said they do not (currently at least) wish to modify import specifiers (f.e. converting .ts to .js), but that they officially support .js.

Thus, using the .js extensions makes importing in native ES Module systems easier, because browsers do not yet support things like import 'some/file': the browser will try to load the equivalent of 'current-file-folder/some/file' instead of 'current-file-folder/some/file.js' and it will fail, unless

  • the server is specifically configured to automatically find the matching file with the added extension, but this isn't default behavior in the average static non-js-specific file server.
  • one uses additional build tooling to insert the .js extensions after TypeScript compilation.

This feature would be compatible with TypeScript, and would allow us to compile to WebAssembly or to plain JavaScript with native ES Modules more easily without the extra workarounds.

Some people will despise the feature, but they don't have to use it. And honestly as an engineer that needs to get things done, it's a perfectly fine solution.

@trusktr trusktr changed the title portability: allow .js in import identifiers. portability: allow .js in import specifiers Sep 11, 2021
@MaxGraey
Copy link
Member

@MaxGraey MaxGraey commented Sep 14, 2021

It seems TS support import .js files only with --allowJs option. And this is quite obvious, since TS is trying to smooth the migration from JavaScript to TypeScript and such features are quite appropriate in TS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment