-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
module: add --experimental-strip-types #53725
module: add --experimental-strip-types #53725
Conversation
Review requested:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 thank you for the great work!
I think correct source locations in the error stacks are required in the short term as well.
One possible solution could be that avoid altering source locations in stripping so that source maps generation and mapping would not be required. Note that source maps come with a cost and it would not be trivial (per memory usage and performance). (idea from @acutmore)
Sourcemaps are already supported by swc and we might just add a flag to enabled them. |
Not supporting extension-less imports is by-design: https://nodejs.org/api/esm.html#mandatory-file-extensions |
This comment was marked as resolved.
This comment was marked as resolved.
@marco-ippolito, speaking as a module maintainer here, it is very common for TS module codebases to have its own file imports written with If I'm to replace |
This is something we definitely want look into, to be able to maintain compatibility with what you run and what you compile. |
Glad it's on your mind then! As a sidenote, would |
It's still in Stage 1 so it will take at least a few years but I hope v8 will implement the api to make it happen 🔥 |
Resolution of If the user wants to use the same source files both run directly via this new flag and run as transpiled JavaScript output by a build tool, it would be the responsibility of the build tool to convert |
e7d70d7
to
7a22f7f
Compare
There are two aspects of TypeScript support here:
AFAICT this does both (although the second one does it in a limited way), so that brings up the question on whether we want to make this this unflagged eventually. If we want to unflag it eventually then 2 is going to be a breaking change for existing TypeScript loaders that also try to modify the resolution rules in their own way, then we should only strip the syntax for files with explicit formats but don't try to override the resolution rules. If we want to keep it flagged as |
|
7a22f7f
to
f6fd494
Compare
In test suite I think we should add some test to know how node will work with that: const X = {} as const; and import type { Config } from 'myConfig.d.ts';
export const config = {} satisfies Config; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neat, I'd like to see extensionless imports work if possible, but I also think this can land as-is and those issues fixed before making it stable.
I see few issues with the module approach:
https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution-for-libraries This is especially important in library code (as many projects out of there simply opt-out of ESM entirely and either create bundles FE-wise, or they compile down to CJS for Node ones) but the number of real world products using this pattern increases consistently every day with CJS being phased out by an ever growing number of projects. I wonder whether the flag could strip types and run |
f6fd494
to
206457c
Compare
@benjamingr this is currently not possible because of this mandatory-file-extensions, it really depends on the esm loader.
@enricopolanski That is something that might be resolved with guessing, as you can immagine it requires node to perform a few expensive operation on the file system and a heuristic attempt to guess the extension. Example:
We will iterate on this and it's tracked in the issue nodejs/loaders#208 |
Landed in 35f92d9 |
Try out the Node.js v23.0.0-nightly202407253de7a4c374 nightly version with CodeSandbox: https://codesandbox.io/p/devbox/node--experimental-strip-types-with-node-js-nightly-dxdtmp?file=%2Findex.ts |
I am curious, is there really no better way of shipping the wasm besides inlining it as base64 in deps/amaro/dist/index.js which is 2.47mb? And what guarantees are there this binary blob won't get backdoored at some point in the future? |
because we build the wasm in our repo https://github.com/nodejs/amaro |
I have have missed a discussion (sorry - there's a lot of comments here) but is the idea to eventually support other typing systems (for example, Flow, Hegel, or whatever comes next after TypeScript) like https://github.com/tc39/proposal-type-annotations proposes, or is this feature focused solely on TypeScript? If it's only going to be for TypeScript, wouldn't it be better to include |
Please lets keep the conversation in the relevant issue nodejs/loaders#217 |
PR-URL: #53725 Refs: nodejs/loaders#217 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruy Adorno <ruy@vlt.sh>
Awesome to see this landing ❤️ |
FYI @nodejs/nodejs-website https://nodejs.org/en/learn/getting-started/nodejs-with-typescript this should be updated ASAP. |
@nodejs/releasers @nodejs/build can we add to the |
I can do it, I've already written part of this page, but is it really necessary because it's not yet in release and not stable? |
can you create an issue for this so we can followup |
This comment has been minimized.
This comment has been minimized.
I think it is fair to mention about this new feature there. But Im also fine deferring to wait until it becomes stable. |
@marco-ippolito should it be an issue on the Build repo? |
I think so yes @nodejs/build |
Sorry, @marco-ippolito! I was busy with other stuff, here we go nodejs/build#3847 |
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: nodejs/loaders#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.Why type stripping
Type stripping as the name suggest, means removing all the
types
, transform the input in a JavaScript module.Becomes:
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.