-
Notifications
You must be signed in to change notification settings - Fork 12.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
Tsconfig option to disallow features requiring transformations which are not supported by Node.js' --strip-types #59601
Comments
#54283 is relevant here |
Feels like a lint rule and not a ts flag I have a strong feeling that node will eventually support everything under |
Discussed a bit with some folks internally and there was possible appetite for this. This has been a longstanding request for other reasons (ideologically purity [complimentary], future-proofing, de facto tool support, etc). A sticking point is what the heck to name it and some suggestions to get the ball rolling would be useful. |
Wohooo! π₯³ So if you feel like poking a little fun at the ideological purity section, call it |
Just to add to @RyanCavanaugh's list of reasons to add this mode: a further benefit not yet stated is that it will permit TypeScript (if the team wishes) to introduce a new JS emit mode that preserves JS syntax coordinates, meaning no sourcemap is required. SWC have already shipped such an emitter written in Rust and compiled to Wasm. @acutmore will soon be open-sourcing another example of such an emitter - this time written in TypeScript. I agree the hardest problem is what to name it. |
I'll just start throwing ideas in:
Note that we almost always prefer a flag like this to be |
I like the direction that |
Highly relevant:
This is early stuff, but it seems like this would probably be the thing to target with such an option. |
Rough notes Who is this for?
Downsides: nothing allowed in .ts exactly replicates what Must use Applies only to .ts, not .d.ts Recommended to combine with Exact definition of what's disallowed and what's not import x = require('fs'); // no (CJS+VMS will not have a good workaround at this time)
import A = e.p; // no
class X {
public x; // OK (just erase `public`)
constructor(public y) { } // not OK - runtime-observable
}
enum X { } // All forms (including `const`) not OK
namespace T { } // OK (type-only)
namespace X { // Not OK (instantiated)
const x = 1;
} |
Thanks for the clear concise comprehensive update, @RyanCavanaugh. namespace T { } // OK (type-only) This was the only surprise to me. I appreciate it does not emit anything so can be considered erasable. I'm curious why anyone would use this form and whether its used in real-life. |
Non-instantiated namespaces are need to do certain kinds of declaration merging, e.g. adding a no-emit |
I still find confusing that
I really like this proposal from this design note. |
declare namespace T {
export function doSomething(): void;
}
console.log(T.doSomething()) Will typecheck, but then crash at runtime. This syntax is designed for declaring things that already exist in the runtime environment (think Compare that to a rule which bans "instantiated namespaces", complaining about the value declaration. |
What about |
The ts-blank-space compiler I mentioned earlier in this issue is now publicly available. One correction to the earlier post is that the performance multiplier relative to classic |
The next step appears to be naming the flag. So here are some options. (Personally I mildly prefer Node-centric
Opt-in Positives
Opt-out NegativesAll these could either be inverted with a
|
I'm personally more into an option that's more descriptive. So from these options I'd go for: While these two makes me think of "typescript will only strip the types" and not "I can't use namespaces" I think it's a good name:
Those I think are more descriptive to what it's actually going to do:
|
Anders had previously suggested I do not think that naming this such that it's related to what Node is doing is a good idea, since it's entirely possible Node decides to instead enable all features supported with isolatedModules (e.g. enums, namespaces), so if we had to change our behavior to match, that would give people who actually want to disable these features for different reasons from having a flag at all. |
Indeed, in addition to |
And one implies the other too, so if you use |
π Search Terms
--strip-types
β Viability Checklist
β Suggestion
Node.js has introduced an experimental flag that allows type annotations to be stripped. However, since Node.js only erases inline types, all TypeScript features that involve replacing TypeScript syntax with new JavaScript syntax will fail as described in the Node.js docs. The following features are listed in the docs as the most important features not supported:
Would it be possible to introduce a single flag in tsconfig that tells the compiler in one fell swoop that all these features should not be enabled to ensure compatibility with Node.js
--strip-types
?π Motivating Example
If there was such a configuration option, you could easily ensure that the code you write always contains only standard JavaScript + type annotations that can be executed by Node.js without installing any additional packages.
π» Use Cases
Finding the correct configuration of tsconfig for different Node.js projects is already relatively complicated. A simplified configuration that allows you to author compliant Typescript code that works smoothly with Node.js new out-of-the-box ts support via the
--strip-types
flag would be a great help.The text was updated successfully, but these errors were encountered: