Node FINALLY Supports TypeScript

Theo - t3β€€gg
28 Jul 202417:51

Summary

TLDRThe script discusses the long-awaited support for TypeScript in Node.js, a feature that has been absent since Node's inception in 2009. It highlights the emergence of alternatives like Dino and Bun, which embraced TypeScript early on. The speaker delves into the complexities of integrating TypeScript, emphasizing the distinction between type checking and transpilation. The introduction of a new Node.js flag that enables type strippingβ€”removing TypeScript syntax without type checkingβ€”is explored. The script also touches on the TC39 proposal for type annotations in vanilla JavaScript and the use of SWC (a Rust-based tool) for implementing this feature, aiming to streamline the JavaScript ecosystem and reduce the need for external dependencies.

Takeaways

  • πŸ•°οΈ Node.js was released in 2009, but it still doesn't natively support TypeScript, which has become the language of choice for the JavaScript ecosystem.
  • πŸ† TypeScript has won the debate over other type systems like Flow, solidifying its position as the preferred choice for JavaScript projects.
  • πŸ”„ Supporting TypeScript is complex because it can mean different things to different people, ranging from type checking to running TypeScript code directly.
  • πŸ“ The TC39 proposal for type annotations aims to integrate TypeScript-like syntax into vanilla JavaScript without the need for transpilation.
  • πŸ› οΈ The goal of the TC39 proposal is to allow developers to run TypeScript, Flow, and other static typing supersets of JavaScript in the browser without modification.
  • 🚫 Previous attempts to enforce types in JavaScript, like the V8 team's 'strong mode' experiment, resulted in performance issues and were eventually abandoned.
  • πŸ›‘ The new Node.js flag for TypeScript is not about type checking but about stripping types and running the resulting JavaScript code.
  • πŸ”§ Node.js will use the 'swc' (Scalable Web Compiler) package for type stripping, leveraging its efficiency and small footprint compared to other solutions.
  • 🌐 The use of WebAssembly for 'swc' allows it to run across all platforms without the need for platform-specific binaries, reducing the size and complexity of Node.js.
  • πŸ“œ The 'Amaro' package, a wrapper around 'swc', will handle the type stripping process in Node.js, making it easier to execute TypeScript files without external dependencies.
  • πŸ“ Node.js will enforce the use of file extensions and module syntax, requiring developers to explicitly use either CommonJS or ES module formats.

Q & A

  • Why is the support for TypeScript in Node.js significant?

    -Support for TypeScript in Node.js is significant because TypeScript has become the language of choice for the JavaScript ecosystem, especially for building tools and projects. Node's support for TypeScript without the need for external dependencies or loaders can streamline development and move the ecosystem forward.

  • What are the two main functions of the TypeScript compiler?

    -The TypeScript compiler has two main functions: type checking, which verifies the correctness of the types used in the code, and transpilation, which converts TypeScript code into JavaScript code.

  • Why was the TC39 proposal for type annotations introduced?

    -The TC39 proposal for type annotations was introduced to add type annotations to the vanilla JavaScript specification in a way that allows developers to run programs written in TypeScript, Flow, and other static typing supersets of JavaScript without the need for transpilation.

  • What is the main goal of the TC39 proposal regarding type annotations?

    -The main goal of the TC39 proposal is to enable developers to run TypeScript, Flow, and other statically typed code in the browser without any need for transpilation, by treating type annotations as a comment syntax rather than a type-checked strict language.

  • What is the difference between type checking and transpilation in the context of TypeScript?

    -Type checking is the process of verifying the correctness of the types used in the code, ensuring there are no type errors. Transpilation is the process of converting TypeScript code, which may include type annotations, into plain JavaScript code that can be executed in a JavaScript runtime environment.

  • Why did the V8 team experiment with a 'strong mode' for JavaScript?

    -The V8 team experimented with a 'strong mode' for JavaScript to enforce types and improve site performance. However, the experiment was canceled due to the non-negligible slowdown it caused, indicating that enforcing types can sometimes lead to more performance problems than it resolves.

  • What is the role of the 'swc' package in the context of Node.js and TypeScript?

    -The 'swc' package, specifically the 'swc-wasm-typescript' package, is used by Node.js for type stripping. It is a Rust-based platform for the web that provides a set of compilation tools to convert TypeScript code into JavaScript code without performing type checking.

  • What is the advantage of using WebAssembly (WASM) for the 'swc-wasm-typescript' package?

    -Using WebAssembly for the 'swc-wasm-typescript' package allows it to run in any environment supported by Node.js without the need for shipping multiple native binaries for different platforms, thus reducing the overall size and complexity of the package.

  • Why is the 'Amaro' package used in Node.js for type stripping?

    -The 'Amaro' package is a wrapper around 'swc-wasm-typescript' and is used for type stripping in Node.js. It allows for the easy integration of the 'swc' transpiler within Node.js, providing a more streamlined and efficient way to handle TypeScript files.

  • What is the significance of file extensions in the new Node.js TypeScript support?

    -File extensions are mandatory for TypeScript files in Node.js to ensure proper module resolution and to avoid conflicts with existing JavaScript files. This is part of Node.js's commitment to maintaining backward compatibility and clarity in module systems.

  • How does Node.js differentiate between CommonJS and ES module syntax when dealing with TypeScript files?

    -Node.js will not automatically convert between CommonJS and ES module syntax. Developers must explicitly use the appropriate import and export syntax for ES modules or require and module.exports for CommonJS modules within their TypeScript files.

Outlines

00:00

πŸ“š The Evolution of Node and TypeScript Support

The script discusses the history of Node.js, which was released in 2009 and still lacks native TypeScript support. It contrasts Node with newer alternatives like Dino and Bun, which have embraced TypeScript early on. The speaker emphasizes the dominance of TypeScript in the JavaScript ecosystem, not just for web applications but also for building tools around Node. The script introduces a new flag in Node that addresses the TypeScript support issue and mentions the tc39 proposal for type annotations in vanilla JavaScript, aiming to allow running TypeScript and other supersets without transpilation.

05:02

πŸ”§ TypeScript's Role in Tooling and the tc39 Proposal

This paragraph delves into the complexities of supporting TypeScript in Node.js, highlighting the different interpretations of 'support.' It explains the two functions of the TypeScript compiler: type checking and transpilation to JavaScript. The speaker discusses the tc39 proposal's goal to integrate type annotations into JavaScript without enforcing them, allowing developers to run TypeScript code in browsers natively. The proposal aims to reduce the friction caused by custom syntax in JavaScript forks and to avoid syntax conflicts with tc39.

10:02

πŸ› οΈ Node's New Flag for TypeScript and the Importance of Transpilation

The script introduces a new flag in Node that enables stripping types from TypeScript, allowing the execution of TypeScript files as JavaScript without type checking. It explains the motivation behind this feature, emphasizing the community's demand for running TypeScript natively in Node. The speaker also touches on the technical aspects of type stripping, using the swc (Scalable Web Compiler) project, a Rust-based tool for efficient transpilation. The adoption of swc's WebAssembly TypeScript package is highlighted for its cross-platform capabilities and smaller size compared to native binaries.

15:02

🌐 The Future of TypeScript in Node and the Amo Package

The final paragraph outlines the future possibilities for TypeScript in Node.js, with the introduction of the Amo package, a wrapper around swc's WebAssembly TypeScript for type stripping. It discusses the benefits of this approach, including reducing the need for multiple binaries and the potential for separate upgrades from Node itself. The script also addresses the requirement for file extensions in import statements for TypeScript files and the ecosystem's efforts to integrate TypeScript support responsibly and effectively.

Mindmap

Keywords

πŸ’‘Node.js

Node.js, often referred to as 'Node', is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a browser. It is the main theme of the video script as it discusses Node's history and its evolving support for TypeScript. The script mentions Node's initial release in 2009 and its current efforts to better integrate with TypeScript, highlighting Node's ongoing development and community engagement.

πŸ’‘TypeScript

TypeScript is a superset of JavaScript that adds optional static types, aiming to provide a more robust development experience. The script discusses TypeScript's victory as the language of choice in the JavaScript ecosystem and its importance in tooling around Node.js. The video also addresses the challenges and complexities of integrating TypeScript with Node.js directly.

πŸ’‘Deno and Bun

Deno and Bun are alternative runtime environments to Node.js, mentioned in the script as having early support for TypeScript. They represent the competitive landscape and the push for better language features in runtime environments, influencing Node.js to consider native TypeScript support.

πŸ’‘Type Annotations

Type annotations in TypeScript are used to explicitly specify the type of a variable, function, or parameter. The script discusses a proposal for type annotations to be part of the JavaScript specification, allowing for type-safe code to run in browsers without transpilation, which is a significant development in the JavaScript ecosystem.

πŸ’‘TC39 Proposal

The TC39 proposal mentioned in the script refers to an effort by the technical committee behind the JavaScript standard to formalize an ergonomic syntax for type annotations in JavaScript. This proposal is crucial for the script's narrative as it aims to bridge the gap between TypeScript and JavaScript, enabling developers to write type-annotated code that runs natively in browsers.

πŸ’‘Transpilation

Transpilation is the process of converting code from one language (or version) to another, often from a newer version of a language to an older one that is more widely supported. The script discusses the need to avoid transpilation for TypeScript, emphasizing the desire for TypeScript code to run natively in browsers and, by extension, in Node.js.

πŸ’‘TypeScript Compiler

The TypeScript compiler is a tool that transforms TypeScript code into JavaScript. The script differentiates between the type-checking aspect of the compiler and the actual compilation process, noting that type errors do not necessarily prevent JavaScript code from being generated. This distinction is important for understanding how the new Node.js flag for TypeScript might work.

πŸ’‘SWC (Speedy Web Compiler)

SWC is a Rust-based platform for the web, which includes a set of compilation tools designed to improve web development speed. The script highlights SWC's use in the new Node.js type stripping feature, which leverages SWC's WebAssembly TypeScript package to convert TypeScript to JavaScript without type checking.

πŸ’‘Enums

Enums, or enumerations, are a way to give more friendly names to sets of numeric values. The script mentions that enums, which are a feature of TypeScript but not originally of JavaScript, will not be automatically converted by the new Node.js feature, indicating a limitation in the initial implementation of TypeScript support in Node.js.

πŸ’‘Type Stripping

Type stripping refers to the removal of type annotations from TypeScript code, leaving behind pure JavaScript code. The script discusses Node.js's new capability to perform type stripping, which allows TypeScript files to be executed without the need for an external TypeScript runtime or loader.

πŸ’‘ES Modules

ES Modules are a standard for module loading and execution in JavaScript, allowing the use of `import` and `export` syntax. The script notes that Node.js will not automatically convert between CommonJS and ES Modules, and that file extensions are mandatory for proper module resolution, which is an important aspect of Node.js's support for TypeScript files.

Highlights

Node.js was released in 2009 but still doesn't natively support TypeScript.

Alternatives to Node like Dino and Bun have embraced TypeScript earlier in their development.

TypeScript has become the language of choice for the JavaScript ecosystem.

Supporting TypeScript varies from type checking to running TypeScript code directly.

Different TS config setups can result in different code outcomes.

Node's new flag aims to address TypeScript support without enforcing type checks.

TC39 proposal for type annotations seeks to integrate TypeScript-like syntax into vanilla JavaScript.

The goal is to allow running TypeScript, Flow, and other supersets of JavaScript without transpilation.

TypeScript's syntax, such as colons for type annotations, is not valid in standard JavaScript.

Efforts to enforce types in JavaScript have resulted in performance issues.

The distinction between TypeScript's type checking and transpilation is crucial for understanding the language's capabilities.

TypeScript files can be transpiled into JavaScript without type checking using tools like TSC.

Node's new feature will strip TypeScript types during transpilation, allowing direct execution of TypeScript files.

SWC, a Rust-based tool, is used for the type stripping process in Node.

SWC's WASM support allows it to run on any platform without native binaries, reducing the size of Node.

Node's strict requirement for file extensions and module syntax aims to maintain compatibility and clarity.

The introduction of type stripping in Node is a significant step towards supporting modern JavaScript practices.

The ecosystem's evolution shows a continuous effort to integrate TypeScript support in a performant and user-friendly manner.

Transcripts

play00:00

it's crazy to think that node came out

play00:01

all the way back in 2009 and technically

play00:03

speaking it still doesn't support

play00:05

typescript this is one of the big things

play00:07

that alternatives to node like Dino and

play00:09

Bun got right early we know that

play00:11

typescript won it's not a debate anymore

play00:13

we're not sincerely considering

play00:15

typescript versus flow anymore

play00:17

typescript is the language of choice for

play00:18

the JavaScript ecosystem now it's not

play00:20

necessarily what we're using in the

play00:21

browser for all of our web apps and

play00:22

whatnot but it is absolutely the

play00:24

language that we're using to build the

play00:25

tools in the ecosystem surrounding the

play00:28

project that we now know as node so why

play00:30

don't we support typescript it's kind of

play00:33

a complex situation because supporting

play00:35

typescript means a different thing to

play00:37

almost everybody for some supporting

play00:39

typescript means checking the types

play00:41

making sure everything is valid and

play00:42

spinning out an error if it's not for

play00:44

others supporting typescript means you

play00:46

can put in typescript code and it runs

play00:48

also important to note that when you

play00:49

have different things like your TS

play00:51

config configured in different ways the

play00:53

result might be entirely different code

play00:55

or will or will not pass how does this

play00:57

all get supported in node it's a great

play01:00

question and we've been going around

play01:01

this for a while the thing we're here to

play01:03

talk about today is this awesome new

play01:05

flag that just got addited to node

play01:06

before we get there we need to talk

play01:08

about an important proposal which is the

play01:09

tc39 proposal for type annotations I've

play01:12

mentioned this once or twice before it's

play01:14

a really important proposal the goal of

play01:16

it is to add type annotations to be part

play01:18

of the vanilla JavaScript spec not in a

play01:20

way that they'll be checked and enforced

play01:22

but in a way that you can copy paste

play01:23

some types of code in the browser and it

play01:25

will just run as expected because right

play01:27

now if I go to my editor and I'll just

play01:29

write create a quick function in this

play01:30

file function add numbers and we have a

play01:35

number B number return if I just copy

play01:37

paste this into the browser we go to

play01:39

console paste uncaught syntax error

play01:43

unexpected token colon because these

play01:45

colons aren't valid syntax in JavaScript

play01:47

this syntax only exists for typescript

play01:50

so I have to delete these and make this

play01:52

function not type safe in order to go

play01:53

Define this and now I can add num is

play01:55

just fine but the fact that you have to

play01:57

do that is a problem in the goal of the

play01:59

ecpt proposal here isn't to make it so

play02:02

type safy is enforced in the browser the

play02:04

goal is to enable developers to run

play02:06

programs that are written in typescript

play02:07

flow and other static typing super sets

play02:09

of JS without any need for transpilation

play02:11

this is the key word and we need to

play02:13

emphasize this one transpilation is most

play02:15

of what we're talking about here this

play02:17

proposal is full of really interesting

play02:19

things let me know if you want a whole

play02:21

video about it the key motivation is

play02:23

they wanted to unfor JavaScript clever

play02:25

pun but the the goal here was to make it

play02:27

so these other languages weren't as far

play02:30

from the JavaScript standard and ideally

play02:32

could all work in the browser the same

play02:33

way JS does the strong demand for

play02:35

ergonomic type annotation syntax has led

play02:37

to Forks of JavaScript with custom

play02:39

syntax this is introduced developer

play02:41

friction and means widely used

play02:43

JavaScript Forks have trouble

play02:44

coordinating with tc39 and must risk

play02:46

syntax conflicts this proposal

play02:48

formalizes an ergonomic syntax space for

play02:50

comments to integrate the needs of type

play02:52

checked Forks of JS so this is almost

play02:55

like colons being used for comments in

play02:57

JS and then they can be used for types

play02:59

everywhere else think of it similar to

play03:01

how JS doc Works where you can leave a

play03:02

comment on top of a function to Define

play03:04

the types for it but as part of the

play03:06

actual typescript in JavaScript syntax

play03:08

we're doing a colon like this leaves a

play03:11

comment and then the comma ends the

play03:13

comment it's a very interesting proposal

play03:15

that has a lot of potentials for wins

play03:17

the reason they're doing this is that

play03:19

very very very many jsds want static

play03:22

typing in JavaScript also pipe operators

play03:25

someday fingers crossed I'll get my

play03:27

beloved pipe operator eventually there's

play03:29

some interesting discussion at the end

play03:30

here about how attempts to actually

play03:32

enforce the types have been done but

play03:34

these have all resulted in

play03:35

non-negligible slowdown it's actually

play03:37

it's fun call it that in 2015 the V8

play03:39

team which is the team that makes the

play03:40

engine that powers both node and chrome

play03:43

they were experimenting with a proposal

play03:44

to implement a new Js mode that they

play03:46

called strong mode think of this kind of

play03:47

like strict mode but stronger where you

play03:49

could enforce types in type checks and

play03:51

they intended to use types to improve

play03:53

site performance Google canel the

play03:54

experiment in failure in the end we had

play03:56

to give up on this one yeah it turns out

play03:57

a lot of these types of things tend to

play04:00

cause more performance problems than

play04:01

they resolve it's hard to get right

play04:03

there are some projects trying to get

play04:04

this right check out my static Hermes

play04:06

video all about the custom JavaScript

play04:08

compiler runtime and whatnot that's goal

play04:10

is to make your JavaScript code as fast

play04:12

as the equivalent assembly code really

play04:14

cool project by facebook/ meta and

play04:16

Amazon to try and make react native apps

play04:18

Just instantaneously Launch and run but

play04:20

these efforts are massive and really

play04:22

rough which is why this particular

play04:24

proposal has the goal of treating

play04:26

typescript like a comment syntax instead

play04:29

of like a type checked strict language I

play04:32

think this is awesome I have seen a

play04:34

problem so many times with how people

play04:36

use typescript that I think I need to

play04:38

excal draw so we have the typescript

play04:40

compiler so we have this high. TS file

play04:43

and what people seem to think happens is

play04:45

this comes out and if all of the syntax

play04:47

here is correct on the other side we get

play04:50

high. JS which is your Javascript file

play04:54

this is not actually how the typescript

play04:56

compiler Works think of the typescript

play04:58

compiler like two Parts one part is the

play05:01

type Checker and the other part is the

play05:03

actual compiler that results in

play05:05

JavaScript code it's important to know

play05:07

that the compiler the type Checker side

play05:09

of it doesn't have to be happy for the

play05:11

JavaScript to come out so you can think

play05:13

of this as there's two parts that come

play05:15

out here it's harder than it looks so

play05:17

one of the two things that comes out of

play05:18

the typescript compiler is the actual

play05:20

output file but the other thing that

play05:22

comes out is any errors you have so I'll

play05:25

make error const hello is undefined

play05:30

so on one hand you might get this file

play05:32

that as far as the compiler knows could

play05:35

work could not work it doesn't know

play05:36

because typescript can have an error on

play05:39

code that is totally valid like if we

play05:42

have a function here we'll just call

play05:43

this add we say a number B number if I

play05:47

call add with Hello World this is a type

play05:51

error but this code works so this is why

play05:54

it's important to understand that these

play05:55

are separate things that the typescript

play05:58

compiler both gives you you errors and

play06:00

anything that it thinks is wrong when it

play06:02

checks the code as well as the

play06:04

compilation step where it actually

play06:05

generates the output JavaScript code the

play06:08

other important thing to know is that

play06:09

doing this part where you turn the

play06:11

typescript into JavaScript is much

play06:13

easier to do than this part where you

play06:15

find errors based on the actual syntax

play06:18

the tree of what's going on in your

play06:20

typescript code this part is effectively

play06:22

transpilation where we're turning high.

play06:25

TS into high. JS we are taking this

play06:28

input and we are turning it into into

play06:29

this output the compiler is also the

play06:31

type Checker which spits out these

play06:33

errors want to call this distinction out

play06:35

because I think it's important to know

play06:37

because right now this proposal isn't a

play06:39

proposal to check the types and give you

play06:41

errors if they're wrong this proposal is

play06:44

to skip the type check and just strip

play06:47

all the types out so you can run the

play06:49

JavaScript code without the typescript

play06:51

syntax breaking it ammed just dropped a

play06:54

good meme I wanted to include quick

play06:55

typescript compiler when type checking

play06:57

here's my code no there's four

play06:59

places where you used a type that could

play07:01

be unfolded TS compiler when transpiling

play07:04

here's my code looks good to me

play07:06

this is javascript's problem now also of

play07:07

note I'm not sure if I mentioned this

play07:09

properly before you don't need to use

play07:10

the TS compiler to do the transpilation

play07:12

the actual TSC JavaScript package is

play07:15

kind of slow and there's a bunch of

play07:16

other things you can use to take that

play07:18

typescript code and turn into JS you can

play07:20

use things like es build swc and so many

play07:24

other things they say undefined I'm

play07:26

sorry I reading is hard okay I'm tired

play07:28

something you'll see on most of my

play07:30

projects in near the end of my tutorials

play07:32

is this set of calls here where I ignore

play07:35

build errors in typescript and I ignore

play07:37

during Builds on esent the reason for

play07:39

this is I don't want my typescript type

play07:41

checking to block my deployments I want

play07:44

the typescript type checking to be done

play07:46

in CI so that I know if there's an issue

play07:48

and it will get flagged before we merge

play07:50

the code and it goes to main but I don't

play07:52

want to type check on my deploy as well

play07:55

as on my CI I'm effectively doing the

play07:57

same thing here that node is doing where

play07:59

I'm not checking my types as I'm turning

play08:01

my typescript into JavaScript I'm just

play08:03

turning it into JavaScript and then I

play08:05

have CI that will actually do the type

play08:08

check I have the pnpm type check and the

play08:10

pnpm lint call here and these run

play08:12

separately because again I don't want to

play08:14

block my deployment on my type checks

play08:16

and if I can have both of these things

play08:17

running separately on different

play08:19

infrastructure it means that my demo app

play08:21

can be out as quick as possible in a PR

play08:23

build and that my actual local like Ci

play08:26

that's running on GitHub can give me the

play08:28

feedback I need so I know I'm not

play08:30

shipping a mistake what's the type check

play08:32

script that's just a super simple

play08:33

package Json TSC D- no emit cuz again I

play08:37

don't want the output code I just want

play08:39

to know that the types are good and this

play08:41

will throw an error if I get the types

play08:42

wrong so the goal of this isn't so much

play08:45

to get you correct errors in your

play08:47

terminal as you're writing code it's to

play08:49

make sure you don't need something like

play08:51

TS node which is a total mess to set up

play08:53

just to run your typescript code and

play08:55

this is how other things already work

play08:57

like if I I'm just going to make a new

play08:59

file hello. TS

play09:02

console.log Su nerds you know what we'll

play09:05

um make this a little more real function

play09:09

hello name String cool well

play09:12

console.log hello world thank you super

play09:14

Maven for flying through this so cool I

play09:17

made this function as a type definition

play09:19

and if I try to run this with node I

play09:21

just do node hello. TS it doesn't know

play09:23

how to deal with the syntax if I do bun

play09:25

hello. TS it doesn't give a Doo run

play09:28

hello. TS same deal yet the idea node's

play09:30

the only one that doesn't know what to

play09:32

do with this sadly this does not seem

play09:34

like something I can easily run right

play09:35

now because it merged literally 5 hours

play09:38

ago so there isn't even a like nightly

play09:40

build up yet hopefully there will be

play09:42

soon but the least we can do now is read

play09:44

through what's going on and what the

play09:45

plan is because I am very excited to

play09:47

have a strip type built into node node

play09:49

will transpile the typescript source

play09:51

code into JavaScript source code during

play09:53

the transpilation process no type

play09:54

checking is performed and types are

play09:56

discarded as it should be checking the

play09:58

types yourself is not that hard as I

play10:00

just showed in this code here it's

play10:01

literally just TSC D- noit now you have

play10:04

your type Checker it's easy to do

play10:05

motivation I believe enabling users to

play10:07

execute typescript files is crucial to

play10:09

move the ecosystem forward it has been

play10:11

requested on all of the surveys and it

play10:12

simply cannot be ignored we must

play10:14

acknowledge that users want to run node

play10:16

food. TS without installing external

play10:18

dependencies or loaders yes absolutely

play10:20

TS nodes become like a meme and it is

play10:23

absolutely deserving of being a meme not

play10:25

because the people who made it or bad

play10:26

faith or anything but the fact that it

play10:27

is necessary is relatively pain painful

play10:30

why type stripping type stripping as the

play10:32

name suggests means removing all of the

play10:33

types transforming the input into a JS

play10:35

module here we see basically the exact

play10:37

same example I did here where we just

play10:38

stripped the colon string other run

play10:40

times also perform transformations of

play10:42

some typescript only features into

play10:43

JavaScript for example enums which do

play10:45

not exist in JS at least initially in

play10:47

this PR no transformation is performed

play10:49

meaning that using enum namespace Etc

play10:51

will not be possible good call out

play10:53

because there are some things in

play10:54

typescript that aren't just stripped

play10:57

like enum becomes something different

play10:58

because there is no concept of enum in

play11:00

JavaScript one of the many reasons you

play11:02

probably shouldn't touch enums but you

play11:04

get the idea anything that's specific to

play11:06

how typescript Works won't make it

play11:09

through this transpilation thankfully

play11:10

typescripts been more hesitant to add

play11:12

features that aren't part of vanilla JS

play11:14

they're a little eager to add some ahead

play11:16

of time like they added Nish coalesence

play11:18

optional chaining and some of those

play11:19

types of features before they made it to

play11:21

core JavaScript but they only add things

play11:23

that have been approved that will be

play11:25

part of CJs in the future which is what

play11:27

makes this interesting because these

play11:29

older features that were added before

play11:31

typescript decided it was going to focus

play11:33

on being as close to JS as possible

play11:35

those won't be supported properly so

play11:36

there will absolutely be some code that

play11:38

doesn't work with this flag at least for

play11:40

now but there's a future where that

play11:41

changes also note is how they're

play11:44

actually doing the type stripping

play11:45

because they didn't write a new solution

play11:46

for this they're actually using s swc

play11:48

specifically the wasm typescript package

play11:51

S swc as described is the rust based

play11:53

platform for the web it's a set of

play11:55

compilation tools written in Rust to try

play11:58

and make web dev faster it was

play11:59

originally just a typescript to

play12:01

JavaScript compiler to take your

play12:03

typescript code and make it into a

play12:04

smaller JS bundle think of this as

play12:06

similar almost to something like es

play12:08

build where it's just the minimal takes

play12:10

the input spits out the output part it's

play12:12

not a proper bundler that handles all of

play12:14

the complexity around getting the right

play12:16

files to the right place making sure

play12:17

that your bundles are split properly all

play12:19

of that it's a top level transpiler

play12:21

almost that turns the typescript code

play12:23

into JS this is what's being used right

play12:25

now inside of nextjs to turn your

play12:26

typescript code into JS that chips to

play12:28

your users s swc is really cool powerful

play12:31

project and it's cool seeing it being

play12:32

adopted in these ways because one of the

play12:34

things they pushed really hard for was

play12:35

wasm support not because they expect you

play12:37

to run s swc in the browser but because

play12:40

they want s swc to work in every

play12:42

environment and since node supports wasm

play12:44

in every environment by supporting WM s

play12:46

swc can run everywhere without having to

play12:48

ship a bunch of binaries this is

play12:50

important because the amount of binaries

play12:52

that have to ship for S swc and

play12:53

something like nextjs is honestly a

play12:56

little bit funny if we go to my node

play12:58

modules next s swc Darwin arm 64 this is

play13:03

the version I need cuz I'm on a Mac but

play13:05

next now has multiple swc versions that

play13:08

I support for 32-bit and 64-bit Windows

play13:11

Mac and Linux arm or Intel so there's a

play13:14

ton of these binaries now that exist in

play13:16

the next package you thankfully only

play13:18

need to install a subset of them but not

play13:20

everything's smart enough to do that

play13:21

right and you might just have seven

play13:23

Darwin or not Darwin s swc binaries

play13:26

floating around on your system now and

play13:28

these aren't small we reveal and finder

play13:30

get info that's 100 megabytes just for

play13:33

the swc arm 64 binding so having a bunch

play13:37

of those for every operating system

play13:39

that's not fun which is why shipping s

play13:41

swc for every single platform as native

play13:44

binaries inside of node makes no sense

play13:46

because now you have to eat that for

play13:48

every single platform and they don't

play13:50

want to do that they don't want to

play13:51

massively increase the size of node but

play13:53

by using the wasm typescript build they

play13:55

have just one and since wasm runs in

play13:57

every platform s swc WM can run on every

play14:00

platform without needing a native binary

play14:02

it won't be as fast as if they were

play14:04

using the native binary but now they

play14:05

have to eat way less like straight up

play14:08

bundle additions and cruft in order to

play14:10

get this working as the author says I've

play14:12

considered other tools but they require

play14:13

either rust or go to be added to the

play14:15

tool chain S swc wasm typescript is a

play14:17

small package with a wasm and ajs file

play14:19

to bind it s swc is currently used by

play14:21

Dino for the same purpose it's battle

play14:23

tested in the future I see this being

play14:25

implemented in a native layer massive

play14:26

shout outs to kd1 for releasing an s swc

play14:29

for us absolutely huge and if we go to

play14:31

npm we can quickly see that the wasm

play14:34

typescript package just got updated

play14:36

first and foremost good to see but more

play14:37

importantly once npm loads you'll see

play14:41

it's 2.5 megabytes versus this s version

play14:45

and S swc Darwin arm 64 much less so 115

play14:50

Megs yeah just a little bit smaller 115

play14:53

Megs to 2.8 yeah sorry 2.5 you get the

play14:57

idea this is pretty much as as

play14:59

responsible of a way as you can

play15:00

Implement something like this I'm

play15:01

honestly really pumped to see it Amaro

play15:04

is a wrapper around swc wasm typescript

play15:06

a web assembly Port it's currently used

play15:08

as an internal and no JS for type

play15:10

stripping the future it will be possible

play15:12

to upgrade

play15:13

separately Aero means bitter in

play15:17

Italian clever very clever so they added

play15:19

this new package called Amo that is the

play15:22

thing that actually does this diff is

play15:24

too large it's 617 lines what the

play15:26

do you mean it's too large it said it

play15:28

was 600 lines oh it's cuz there's a

play15:30

giant binary here okay I take it back

play15:32

this is a little absurd is that how

play15:34

they're including the package they just

play15:36

took all of it and put it in as text

play15:39

that's serialized I mean clever that

play15:42

solves the problem yeah they they parse

play15:45

this giant blob as a web assembly module

play15:49

and that's how they intergrate it that

play15:51

is kind of hilarious I won't lie who

play15:53

needs to deal with things as complex as

play15:55

npm and modules when you can just dump

play15:59

all of the contents as text and then

play16:02

base 64 encode it and then we have the

play16:03

dev depths here and you'll notice that

play16:06

we don't have S swc as one of the depths

play16:08

because they just dumped the source

play16:09

there not even the source the like

play16:11

output binary code that's super cool

play16:14

it's always fun reading through these

play16:15

node PRS they're full of fascinating

play16:17

things node supports both commonjs and

play16:19

Es module syntax and typescript files

play16:21

node does node will not convert from one

play16:23

module system to another if you want

play16:25

your code to run as an es module you

play16:26

must Import and Export syntax and if you

play16:29

want it to run as commonjs you must use

play16:30

require and module. exports as in

play16:32

JavaScript files file extensions are

play16:34

mandatory and import statements and

play16:36

import Expressions import file TS not

play16:38

import file because of backwards

play16:40

compatibility file extensions are also

play16:41

mandatory and require interesting this

play16:45

won't work if you're not using the TS or

play16:48

JS syntax I never thought they would go

play16:51

that hard on that in node it's the right

play16:53

decision I know that typescripts been

play16:54

pushing Us in this direction for a while

play16:56

but I think most people are going to be

play16:58

really weird out the first time they see

play17:00

in a file like this common button. TSX

play17:03

and as you see here that just breaks for

play17:05

me which is funny because typescripts

play17:08

official recommendation is doing that I

play17:10

think I have a video about that already

play17:11

if not let me know and I'll consider

play17:13

doing one so that we can cover that very

play17:16

very interesting controversial change

play17:18

but until then I think I've covered all

play17:19

I have to say on this one this is an

play17:21

awesome change I am genuinely really

play17:23

excited to see it crazy to think that

play17:24

typ scp's been out for 12 years and

play17:26

node's been out for 14 and we're just

play17:28

now getting official typescript

play17:29

supported node but it shows once again

play17:32

how hard the ecosystem is trying to do

play17:34

these things right I don't want to fault

play17:36

node or make it seem like they're a

play17:37

failure of any form for not supporting

play17:39

this before they let the ecosystem do

play17:40

its thing and now that we've done our

play17:42

thing they're finding the best possible

play17:44

way to integrate it into the platform so

play17:46

let me know what you guys think in the

play17:47

comments and until next time keep using

play17:49

bun peace nerds

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Node.jsTypeScriptJavaScriptEcosystemTC39Type AnnotationsTranspilationWeb DevelopmentStatic TypingSWC