-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Add verbatimModuleSyntax
, deprecate importsNotUsedAsValues
and preserveValueImports
#52203
Add verbatimModuleSyntax
, deprecate importsNotUsedAsValues
and preserveValueImports
#52203
Conversation
That’s right; you’ll want to use typescript-eslint’s |
Okay cool - will do. Was it more trouble than it was worth to maintain this with CommonJS then? |
|
Interesting. Yeah I always mentally modelled |
There is no mention of this option in https://www.typescriptlang.org/tsconfig (same for all other new options from TS 5.0), is it expected ? |
@xfournet thanks, I opened microsoft/TypeScript-Website#2759 |
…roken Need to make some decisions about whether/how to emit CJS modules, because as long as we have verbatimModuleSyntax enabled, we cannot use the typical `export const = "x"` format, and tsc-multi rewriting is also now breaking for this on v5, and patching it to do the rewrites may be non trivial since it can involve shifting large blocks of code. Alternatively we could just not opt into the flag, in which case we should also restore the `isolatedModules` flag (which was removed as redunant). See: * https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#verbatimmodulesyntax * microsoft/TypeScript#52203 * tommy351/tsc-multi#19
This PR upgrades the Apollo Server repo to use TS 5.x. This change suggests we drop `importsNotUsedAsValues` in favor of the new [`verbatimModuleSyntax`](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#verbatimmodulesyntax). However, this rule [isn't really recommended or expected to be adopted by CommonJS packages](microsoft/TypeScript#52203 (comment)). So instead of opting in to the new option, I've taken the recommendation in the comment to enforce this via a lint rule which seems to have accomplished the thing we hoped to get out of `importsNotUsedAsValues` in the first place (but better, for some reason?). --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…roken Need to make some decisions about whether/how to emit CJS modules, because as long as we have verbatimModuleSyntax enabled, we cannot use the typical `export const = "x"` format, and tsc-multi rewriting is also now breaking for this on v5, and patching it to do the rewrites may be non trivial since it can involve shifting large blocks of code. Alternatively we could just not opt into the flag, in which case we should also restore the `isolatedModules` flag (which was removed as redunant). See: * https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#verbatimmodulesyntax * microsoft/TypeScript#52203 * tommy351/tsc-multi#19
becaure --importsNotUsedAsValues are being deprecated, more details can be found at microsoft/TypeScript#52203
This PR upgrades the Apollo Server repo to use TS 5.x. This change suggests we drop `importsNotUsedAsValues` in favor of the new [`verbatimModuleSyntax`](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#verbatimmodulesyntax). However, this rule [isn't really recommended or expected to be adopted by CommonJS packages](microsoft/TypeScript#52203 (comment)). So instead of opting in to the new option, I've taken the recommendation in the comment to enforce this via a lint rule which seems to have accomplished the thing we hoped to get out of `importsNotUsedAsValues` in the first place (but better, for some reason?). --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This PR upgrades the Apollo Server repo to use TS 5.x. This change suggests we drop `importsNotUsedAsValues` in favor of the new [`verbatimModuleSyntax`](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#verbatimmodulesyntax). However, this rule [isn't really recommended or expected to be adopted by CommonJS packages](microsoft/TypeScript#52203 (comment)). So instead of opting in to the new option, I've taken the recommendation in the comment to enforce this via a lint rule which seems to have accomplished the thing we hoped to get out of `importsNotUsedAsValues` in the first place (but better, for some reason?). --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Closes #51479
This implements pretty much exactly what I proposed in #51479 so I won’t fully re-explain it here. Instead, I’ll call attention to a couple things I didn’t discuss in the proposal.
CommonJS module exports are severely limited
CommonJS JavaScript modules use
require
andmodule.exports =
for importing/exporting code. TypeScript has direct equivalents for the basic forms of these:However, TypeScript lacks direct equivalents for destructuring a
require
or doing property assignment exports:It is common to approximate this behavior with ESM syntax in TypeScript:
But this is, in fact, ESM syntax trying to represent a CommonJS module, and is not allowed under
verbatimModuleSyntax
. The direct translation is:We knew this would be a bit of a burden for imports, but I didn’t realize what a pain it would be for exports. The problem here is that you can no longer sidecar-export a type:
To accomplish this, you’d have to merge a namespace declaration containing the types:
These limitations make it really unattractive to use the flag in CJS-emitting TypeScript. I discussed this with @DanielRosenwasser, and ultimately decided that it’s not worth worrying about too much—the flag is opt-in, and we expect the users interested in using it to write mostly ESM-emitting TypeScript.
Improved auto-imports for CommonJS imports
Despite the above, I did spend a couple minutes to make auto-imports in CJS files work a bit better. Since
import { writeFile } from "fs"
is not allowed in CJS files, when you start typingwriteFile
, you now get an auto import offs
and the qualification onwriteFile
at the same time:🎥 GIF
Pending codefix
I ensured auto-imports work well with the new mode, but I haven’t yet added any codefixes or reviewed the existing ones—I think there are existing ones to add type-only annotations to imports as necessary, but I’ll likely need to add some for transforming imports between CJS and ESM syntaxes. I wanted to get this PR up so it has time to be reviewed before the beta, but I plan to add codefixes so hopefully codebases can be transitioned onto the flag with ts-fix.