r/typescript • • Mar 09 '23

TypeScript's Migration to Modules

https://devblogs.microsoft.com/typescript/typescripts-migration-to-modules
134 Upvotes

13 comments sorted by

8

u/seemslikesalvation Mar 10 '23

Is there a name for the almost-but-not-quite-ESM syntax that most projects actually use -- importing Node modules by name like import foo from '@some/module', or from a folder that has an index.[jt]sx? like import {foo} from './path/to/folder'?

I've seen it called Harmony modules, but then I've also read that Harmony was just the name for ESM before ESM was standardized.

30

u/DanielRosenwasser Mar 10 '23

I don't think there's a precise name (which is kind of unfortunate) - but I'll try to tease out the question a bit.

Technically ESM (the parts of the ES spec at least) specifies only the syntax and some of the linking/evaluation semantics of modules. Path resolution/loading is implementation defined. (I'm using some terms loosely here). So it's really "is there a specific name for how paths these kinds of paths (non-relative imports, extensionless imports)

but it actually goes beyond that - because Node.js supports different resolution rules across different versions, and bundlers often diverge a little bit too.

So TypeScript 5.0 now has

  • --moduleResoluton node10

  • --moduleResoluton node16

  • --moduleResoluton bundler

with a bunch of new resolution capabilities.

And that's why it's unfortunate - is that not only is there not a name, there's not even a single version of how this works.

7

u/robpalme Mar 10 '23

There is no official name for the Node-like path resolution implemented by bundlers and other JavaScript runtimes.

I call it "Nodey"

6

u/Bake_Jailey Mar 10 '23 edited Mar 10 '23

The syntax I'd definitely call "ESM-style", but the idea of using "index modules" I would say is "node-style resolution".

1

u/[deleted] Apr 30 '23

In your tsconfig.json ModuleResolution: "nodenext", lib: ["ESNext, "DOM"]

You're welcome

2

u/serg06 Mar 10 '23

Typos:

backard-compatible

there’s way less fewer "downsides"

3

u/DanielRosenwasser Mar 10 '23

Fixed, thank you!

50

u/del_rio Mar 09 '23

TS itself using esbuild for bundling and compiling is a massive endorsement for the library, and quite the performance win, too!

19

u/lifeeraser Mar 10 '23

var is faster than let

Premature optimization! Premature optimization!

J/K. This was a really insightful article. Thanks to the TypeScript team for creating and maintaining such a wonderful tool.

6

u/NewLlama Mar 09 '23

11

u/stevedorries Mar 09 '23

Wouldn’t a single tab increase those savings even further though?

9

u/Bake_Jailey Mar 10 '23

It would, but esbuild hardcodes two spaces, so that's what we get.

(And I wasn't about to post-process the output like that; makes source maps break and making them not break is a lot of extra time/effort. Moderately pointless when the package compresses so well.)

2

u/kopps1414 Mar 10 '23

Y'all are the best - thanks Jake & Daniel! For the continued great work, and then the super informative posts like these 🙇