Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

AddverbatimModuleSyntax, deprecateimportsNotUsedAsValues andpreserveValueImports#52203

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

andrewbranch
Copy link
Member

@andrewbranchandrewbranch commentedJan 12, 2023
edited
Loading

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 userequire andmodule.exports = for importing/exporting code. TypeScript has direct equivalents for the basic forms of these:

// JSconstfs=require("fs");module.exports={};// TSimportfs= require("fs");export={};

However, TypeScript lacks direct equivalents for destructuring arequire or doing property assignment exports:

// JSconst{ writeFile}=require("fs");module.exports.hello="world";

It is common to approximate this behavior with ESM syntax in TypeScript:

import{writeFile}from"fs";exportconsthello="world";

But this is, in fact, ESM syntax trying to represent a CommonJS module, and is not allowed underverbatimModuleSyntax. The direct translation is:

importfs= require("fs");// must use `fs.writeFile` or assign to a variableexport={hello:"world"};

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:

exporttypeT={};export={hello:"world"};// ^^^^^^^^^^^^^^^^^^^^^^^^// An export assignment cannot be used in a module with other exported elements.(2309)

To accomplish this, you’d have to merge a namespace declaration containing the types:

const_exports={hello:"world"};namespace_exports{exporttypeT={};}export=_exports;

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. Sinceimport { 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

Kapture 2023-01-11 at 16 31 39

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.

fatcerberus, robpalme, zyhou, nyngwang, and treblasft reacted with hooray emojikachick reacted with eyes emoji
@typescript-bottypescript-bot added Author: Team For Milestone BugPRs that fix a bug with a specific milestone labelsJan 12, 2023
@andrewbranchandrewbranch marked this pull request as ready for reviewJanuary 12, 2023 01:06
@johnnyreilly
Copy link

Okay cool - will do. Was it more trouble than it was worth to maintain this with CommonJS then?

@andrewbranch
Copy link
MemberAuthor

importsNotUsedAsValues was made to serve the opposite purpose you (and basically everyone) were using it for. By default, TypeScript elides unneeded import statements from JS emit even without marking them astype imports.importsNotUsedAsValues was created as a way toescape that behavior, not (primarily) to make it more explicit.verbatimModuleSyntax allows you to escape the elision behavior, and takes the explicitness of what your imports mean a step further by making you write CJS-style imports when emitting to CJS. So in my book, all the important cases thatimportsNotUsedAsValues (andpreserveValueImports) covered, plus more, are covered byverbatimModuleSyntax, which is way more explainable. It’s mostly a matter of reducing complexity for the sake of explanation.

@johnnyreilly
Copy link

Interesting. Yeah I always mentally modelled"importsNotUsedAsValues": "error" as"strictTypeImports": true - essentially it was making code more explicit for me. I guess this is a linting concern rather than a compilation concern - I shall make use ofhttps://typescript-eslint.io/rules/consistent-type-imports/

andrewbranch and stefnotch reacted with thumbs up emoji

@xfournet
Copy link

There is no mention of this option inhttps://www.typescriptlang.org/tsconfig (same for all other new options from TS 5.0), is it expected ?

jraoult and robinwittkamp reacted with eyes emoji

@andrewbranch
Copy link
MemberAuthor

@xfournet thanks, I openedmicrosoft/TypeScript-Website#2759

xfournet reacted with thumbs up emoji

@PolariTOONPolariTOON mentioned this pull requestMar 28, 2023
5 tasks
milesrichardson added a commit to splitgraph/madatdata that referenced this pull requestMar 28, 2023
…rokenNeed to make some decisions about whether/how to emit CJS modules,because as long as we have verbatimModuleSyntax enabled, we cannotuse the typical `export const = "x"` format, and tsc-multi rewritingis also now breaking for this on v5, and patching it to do the rewritesmay be non trivial since it can involve shifting large blocks of code.Alternatively we could just not opt into the flag, in which casewe should also restore the `isolatedModules` flag (which was removedas redunant).See:*https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#verbatimmodulesyntax*microsoft/TypeScript#52203*tommy351/tsc-multi#19
trevor-scheer added a commit to apollographql/apollo-server that referenced this pull requestMar 29, 2023
This PR upgrades the Apollo Server repo to use TS 5.x.This change suggests we drop `importsNotUsedAsValues` in favor of thenew[`verbatimModuleSyntax`](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#verbatimmodulesyntax).However, this rule [isn't really recommended or expected to be adoptedby CommonJSpackages](microsoft/TypeScript#52203 (comment)).So instead of opting in to the new option, I've taken the recommendationin the comment to enforce this via a lint rule which seems to haveaccomplished 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>
milesrichardson added a commit to splitgraph/madatdata that referenced this pull requestApr 10, 2023
…rokenNeed to make some decisions about whether/how to emit CJS modules,because as long as we have verbatimModuleSyntax enabled, we cannotuse the typical `export const = "x"` format, and tsc-multi rewritingis also now breaking for this on v5, and patching it to do the rewritesmay be non trivial since it can involve shifting large blocks of code.Alternatively we could just not opt into the flag, in which casewe should also restore the `isolatedModules` flag (which was removedas redunant).See:*https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#verbatimmodulesyntax*microsoft/TypeScript#52203*tommy351/tsc-multi#19
linhe0x0 added a commit to linhe0x0/tsconfig that referenced this pull requestApr 17, 2023
becaure --importsNotUsedAsValues are being deprecated, more details canbe found atmicrosoft/TypeScript#52203
trevor-scheer added a commit to apollographql/apollo-server that referenced this pull requestApr 17, 2023
This PR upgrades the Apollo Server repo to use TS 5.x.This change suggests we drop `importsNotUsedAsValues` in favor of thenew[`verbatimModuleSyntax`](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#verbatimmodulesyntax).However, this rule [isn't really recommended or expected to be adoptedby CommonJSpackages](microsoft/TypeScript#52203 (comment)).So instead of opting in to the new option, I've taken the recommendationin the comment to enforce this via a lint rule which seems to haveaccomplished 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>
trevor-scheer added a commit to apollographql/apollo-server that referenced this pull requestApr 17, 2023
This PR upgrades the Apollo Server repo to use TS 5.x.This change suggests we drop `importsNotUsedAsValues` in favor of thenew[`verbatimModuleSyntax`](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#verbatimmodulesyntax).However, this rule [isn't really recommended or expected to be adoptedby CommonJSpackages](microsoft/TypeScript#52203 (comment)).So instead of opting in to the new option, I've taken the recommendationin the comment to enforce this via a lint rule which seems to haveaccomplished 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>
cloudcome added a commit to FrontEndDev-org/openapi-axios that referenced this pull requestApr 24, 2023
mnapthine added a commit to action-is-hope/shelley that referenced this pull requestMay 19, 2023
ras0q added a commit to traPtitech/traQ_S-UI that referenced this pull requestMay 22, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@weswighamweswighamweswigham approved these changes

@sheetalkamatsheetalkamatsheetalkamat approved these changes

@RyanCavanaughRyanCavanaughAwaiting requested review from RyanCavanaugh

@sandersnsandersnAwaiting requested review from sandersn

Assignees

@andrewbranchandrewbranch

Labels
Author: TeamFor Milestone BugPRs that fix a bug with a specific milestone
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Proposal: deprecateimportsNotUsedAsValues andpreserveValueImports in favor of single flag
7 participants
@andrewbranch@johnnyreilly@jakebailey@xfournet@weswigham@sheetalkamat@typescript-bot

[8]ページ先頭

©2009-2025 Movatter.jp