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

Docs: Difference between esnext, es6, es2015 module targets #24082

Closed
bcherny opened this issue May 12, 2018 · 19 comments
Closed

Docs: Difference between esnext, es6, es2015 module targets #24082

bcherny opened this issue May 12, 2018 · 19 comments
Labels
Docs The issue relates to how you learn TypeScript

Comments

@bcherny
Copy link

bcherny commented May 12, 2018

Search Terms

esnext, es6, es2015, module, tsconfig, tsconfig.json, difference, document

Suggestion

It would be great if this can be documented. On the face of it, the output for all three seems to be identical.

Related: It would also be nice to document why targeting module: "none" when your code is a module (ie. uses ES2015 imports/exports) generates CommonJS-compatible code instead of exporting to globals as "none" seems to imply.

@DanielRosenwasser DanielRosenwasser added the Docs The issue relates to how you learn TypeScript label May 12, 2018
@DanielRosenwasser
Copy link
Member

To just answer the question, the difference is that import() expressions are understood in esnext, but not in es2015 (and as of TypeScript 2.9, import.meta is only understood with esnext).

I guess you could possibly expect an es2019 module target for both.

@mhegazy
Copy link
Contributor

mhegazy commented May 14, 2018

in addition to what @DanielRosenwasser mentioned, esNext is just a place holder for features that are on the standard track but is not in an official ES spec yet. e.g. import.meta and import() expressions. as TC39 adds these features to a versioned spec, we will add a new --module ES20** to reflect that.

Also es6 === es2015.

@qm3ster
Copy link

qm3ster commented Aug 29, 2018

Is there a place where I can see the list of https://github.com/tc39/proposals syntax supported?
(As opposed to module target or libs.d.ts?)

@zoltan-nz
Copy link

You can find all the libs and supported features in this folder: https://github.com/Microsoft/TypeScript/tree/master/lib

@gilamran
Copy link

The only place that I found for understanding TS support is here, but this table is misleading... "red" doesn't mean that TS doesn't support the feature, it means that it doesn't run on previous version of JavaScript... yea, misleading.

Can we create a similar solution for TS, but with real supported/unsupported table? (With relation to the target)

@ayqy
Copy link

ayqy commented Mar 26, 2019

Is there someone who can give a detail explanation for module: "none" ?
ref: Difference between typescript module “None” and “CommonJS”

@vegerot
Copy link

vegerot commented Feb 11, 2020

Is targeting ESNext or module ESNext stable for long term releases? What if I want import() but also want stability?

@Mikilll94
Copy link

@vegerot
Any answer to this question?

@vegerot
Copy link

vegerot commented Aug 20, 2020

I wish

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 20, 2020

If you're looking for stability, you may want to hold off on using --target esnext unless you're piping the output through another compiler that supports newer ESNext syntax like Babel. If a feature is only available under ESNext, you'll likely need to wait until it is clear which version of the ECMAScript standard it will be added to.

@vegerot
Copy link

vegerot commented Aug 21, 2020

If, say the end result we send to users is ES2016 compatible (say, through Babel), does it make sense to target ES2016 and then pipe through the other compiler? Or to target ESNext and pipe it through the other compiler (assuming the other compiler supports ESNext)?

@DanielRosenwasser
Copy link
Member

If you're targeting a minimally ES2016 environment (most modern browsers, most recent versions of Node) then there's no need to pipe through Babel except for auto-polyfills.

If you need auto-polyfilling, emit to ESNext and pipe through Babel.

If not, just emit to ES2016 with TypeScript.

@scarcoco
Copy link

tsconfig.json module options: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext',what's different of 'es2020', or 'ESNext'?

@stephen-dahl
Copy link

@scarcoco I am guessing from reading this that es2020 is when the import() and import.meta landed. I am just guessing though. ESNext is all the new features that have not been finalized yet. there may or may not be any difference between that and es2020 at the time of writing but in the future when more features are inevitably added to the module system ESNext would include those.

@thernstig
Copy link

Top-level await was included in ECMAScript 2022, see https://github.com/tc39/proposals/blob/main/finished-proposals.md

But there is no "module": "ES2022", so one is forced to use "module": "esnext". But next seems dangerous to me as in a few years from now it might mean new features we do not support yet due to not having upgraded Node.js.

It's not clear for me why there is no ES2022 to support top-level await without having to resort to next.

agilgur5 added a commit to agilgur5/rollup-plugin-typescript2 that referenced this issue Jul 4, 2022
- it's already been supported since eb1dd17, but the docs and error message were not updated to mention it
  - so add both to make sure users aren't confused
  - also re-order it to be ES2015, ES2020, then ESNext consistently, which is their module order (c.f. microsoft/TypeScript#24082)

- modify test to account for the new error message
  - make it a bit more resilient to change by only testing a substring as well (c.f. https://jestjs.io/docs/expect#tothrowerror)
agilgur5 added a commit to agilgur5/rollup-plugin-typescript2 that referenced this issue Jul 4, 2022
- it's already been supported since eb1dd17, but the docs and error message were not updated to mention it
  - so add both to make sure users aren't confused
  - also re-order it to be ES2015, ES2020, then ESNext consistently, which is their module order (c.f. microsoft/TypeScript#24082)

- modify test to account for the new error message
  - make it a bit more resilient to change by only testing a substring as well (c.f. https://jestjs.io/docs/expect#tothrowerror)
ezolenko pushed a commit to ezolenko/rollup-plugin-typescript2 that referenced this issue Jul 6, 2022
- it's already been supported since eb1dd17, but the docs and error message were not updated to mention it
  - so add both to make sure users aren't confused
  - also re-order it to be ES2015, ES2020, then ESNext consistently, which is their module order (c.f. microsoft/TypeScript#24082)

- modify test to account for the new error message
  - make it a bit more resilient to change by only testing a substring as well (c.f. https://jestjs.io/docs/expect#tothrowerror)
@bennycode
Copy link
Contributor

I recently noticed that import assertions are supported with module esnext and nodenext, but not with es2022 (tested with TypeScript 5.1.6). I assume that is because ESNext is continuously evolving but ES2022 is not?

@thernstig it looks like "module": "es2022" will allow you now to use top-level await (tested with TypeScript 5.1.6):

Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘es2022’, ‘esnext’, ‘system’, or ‘nodenext’, and the ‘target’ option is set to ‘es2017’ or higher.

@RyanCavanaugh
Copy link
Member

New module docs are live at https://www.typescriptlang.org/docs/handbook/2/modules.html

@catamphetamine
Copy link

New module docs are live at https://www.typescriptlang.org/docs/handbook/2/modules.html

I'd suggest the following link: https://www.typescriptlang.org/tsconfig#module

@mrdulin
Copy link

mrdulin commented Jul 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

No branches or pull requests