module: add --experimental-strip-types
Moderation Note: This PR has been posted on several social network platforms and thus attracts a lot of mostly off-topic comments.
If you've used this feature and ran into issues or have specific feedback to provide - please open a new issue https://github.com/nodejs/node/issues/new/choose
It is possible to execute TypeScript files by setting the experimental flag --experimental-strip-types
.
Node.js will transpile TypeScript source code into JavaScript source code.
During the transpilation process, no type checking is performed, and types are discarded.
Roadmap
Refs: https://github.com/nodejs/loaders/issues/217
Motivation
I believe enabling users to execute TypeScript files is crucial to move the ecosystem forward, it has been requested on all the surveys, and it simply cannot be ignored. We must acknowledge users want to run node foo.ts
without installing external dependencies or loaders.
There is a TC39 proposal for type annotations
Why type stripping
Type stripping as the name suggest, means removing all the types
, transform the input in a JavaScript module.
const foo: string = "foo";
Becomes:
const foo = "foo";
Other runtimes also perform transformation of some TypeScript only features into JavaScript, for example enums, which do not exists in JavaScript.
At least initially in this PR no trasformation is performed, meaning that using Enum
, namespaces
etc... will not be possible.
Why I chose @swc/wasm-typescript
Because of simplicity.
I have considered other tools but they require either rust or go to be added to the toolchain.
@swc/wasm-typescript
its a small package with a wasm and a js file to bind it.
Swc is currently used by Deno for the same purpose, it's battle tested.
In the future I see this being implemented in native layer.
Massive shoutout to @kdy1 for releasing a swc version for us.
typescript.md
for implementation details and limitations.