GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add `verbatimModuleSyntax`, deprecate `importsNotUsedAsValues` and `preserveValueImports` by andrewbranch · Pull Request #52203 · microsoft/TypeScript

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Jan 12, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

// JS
const fs = require("fs");
module.exports = {};
// TS
import fs = require("fs");
export = {};

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

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

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

import { writeFile } from "fs";
export const hello = "world";

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

import fs = require("fs");
// must use `fs.writeFile` or assign to a variable
export = { 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:

export type T = {};
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 {
  export type T = {};
}
export = _exports;

These limitations make it really unattractive to use the flag in CJS-emitting TypeScript. I discussed this with Daniel Rosenwasser (@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 typing writeFile, you now get an auto import of fs and the qualification on writeFile 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fatcerberus, robpalme, zyhou, nyngwang, and treblasft reacted with hooray emoji kachick reacted with eyes emoji
All reactions
  • 🎉 5 reactions
  • 👀 1 reaction
Andrew Branch (andrewbranch) added 17 commits December 7, 2022 15:39
@andrewbranch
Add flag and update diagnostic messages
5ebc606
@andrewbranch
Finish updating error messages
c1de572
@andrewbranch
Add tests for loosened isolatedModules script checks
cf5c284
@andrewbranch
Update emit, add test
a612746
@andrewbranch
Add elision tests, restrictions for export=
7e8dc51
@andrewbranch
Start adding syntax restrictions
2ebbb97
@andrewbranch
Finish(?) syntax restrictions
5095a7e
@andrewbranch
Update other references to compilerOptions.isolatedModules
0095a8e
@andrewbranch
Merge branch 'main' into feature/verbatimModuleSyntax
c328062
@andrewbranch
Merge branch 'main' into feature/verbatimModuleSyntax
8816d0e
@andrewbranch
Auto-imports for CJS files
915c780
@andrewbranch
Merge branch 'main' into feature/verbatimModuleSyntax
7bbbe9e
@andrewbranch
Ensure ESM imports are type-only when possible
cde58e3
@andrewbranch
Update baselines after merge
2e0acb2
@andrewbranch
Disable marking stuff referenced since it’s not needed
626820c
@andrewbranch
Deprecate importsNotUsedAsValues and preserveValueImports
f15d610
@andrewbranch
Clean up redundancy errors
addcca8
@typescript-bot TypeScript Bot (typescript-bot) assigned Andrew Branch (andrewbranch) Jan 12, 2023
@typescript-bot TypeScript Bot (typescript-bot) added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jan 12, 2023
@andrewbranch
Andrew Branch (andrewbranch) marked this pull request as ready for review January 12, 2023 01:06
@andrewbranch
Andrew Branch (andrewbranch) requested review from Ryan Cavanaugh (RyanCavanaugh), Nathan Shively-Sanders (sandersn), Sheetal Nandi (sheetalkamat) and Wesley Wigham (weswigham) January 12, 2023 01:06
weswigham
Wesley Wigham (weswigham) reviewed Jan 17, 2023
View reviewed changes
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Andrew Branch (andrewbranch) requested a review from Wesley Wigham (weswigham) January 19, 2023 20:12
weswigham
Wesley Wigham (weswigham) reviewed Jan 19, 2023
View reviewed changes
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

weswigham
Wesley Wigham (weswigham) reviewed Jan 19, 2023
View reviewed changes
Comment thread src/services/codefixes/importFixes.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

weswigham
Wesley Wigham (weswigham) approved these changes Jan 19, 2023
View reviewed changes
@andrewbranch

Andrew Branch (andrewbranch) commented Mar 20, 2023

Copy link
Copy Markdown
Member Author

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 as type imports. importsNotUsedAsValues was created as a way to escape 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 that importsNotUsedAsValues (and preserveValueImports) covered, plus more, are covered by verbatimModuleSyntax, which is way more explainable. It’s mostly a matter of reducing complexity for the sake of explanation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnnyreilly

John Reilly (johnnyreilly) commented Mar 20, 2023

Copy link
Copy Markdown

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 of https://typescript-eslint.io/rules/consistent-type-imports/

andrewbranch and stefnotch reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@xfournet

Xavier Fournet (xfournet) commented Mar 24, 2023

Copy link
Copy Markdown

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 ?

jraoult and robinwittkamp reacted with eyes emoji
All reactions
  • 👀 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented Mar 24, 2023

Copy link
Copy Markdown
Member Author

Xavier Fournet (@xfournet) thanks, I opened microsoft/TypeScript-Website#2759

xfournet reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gciotola Giuseppe Ciotola (gciotola) mentioned this pull request Mar 27, 2023
Update typescript and linter to latest commercelayer/app-orders#15
Merged
This was referenced Mar 27, 2023
chore(deps): update dependency typescript to v5 apollographql/datasource-rest#176
Merged
Upgrade to TypeScript 5.x apollographql/apollo-server#7454
Merged
@PolariTOON PolariTOON mentioned this pull request Mar 28, 2023
export default type #41409
Open
4 of 5 tasks
Miles Richardson (milesrichardson) added a commit to splitgraph/madatdata that referenced this pull request Mar 28, 2023
@milesrichardson
Upgrade to TypeScript 5.0, get typechecking working but build still b…
a4d0b84
…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
Trevor Scheer (trevor-scheer) added a commit to apollographql/apollo-server that referenced this pull request Mar 29, 2023
@trevor-scheer @renovate
Upgrade to TypeScript 5.x (#7454)
f6e3ae0
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 was referenced Apr 6, 2023
Allow isolatedModules with global scripts, and better match build tool capabilities #46295
Closed
isolatedModules option is not work in ^5.0.0 version #53683
Closed
Miles Richardson (milesrichardson) added a commit to splitgraph/madatdata that referenced this pull request Apr 10, 2023
@milesrichardson
Upgrade to TypeScript 5.0, get typechecking working but build still b…
c74ae77
…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
@brendaHuang-2023 brendaHuang-2023 mentioned this pull request Apr 11, 2023
Enhancement: update ts-bases to support typescript >=5.0 rehearsal-js/rehearsal-js#946
Closed
Lin He (linhe0x0) added a commit to linhe0x0/tsconfig that referenced this pull request Apr 17, 2023
@linhe0x0
feat: add support for verbatimModuleSyntax option
57e8de0
becaure --importsNotUsedAsValues are being deprecated, more details can
be found at microsoft/TypeScript#52203
Trevor Scheer (trevor-scheer) added a commit to apollographql/apollo-server that referenced this pull request Apr 17, 2023
@trevor-scheer @renovate
Upgrade to TypeScript 5.x (#7454)
bd43410
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>
Trevor Scheer (trevor-scheer) added a commit to apollographql/apollo-server that referenced this pull request Apr 17, 2023
@trevor-scheer @renovate
Upgrade to TypeScript 5.x (#7454)
7319864
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>
☁️淡然 (cloudcome) added a commit to FrontEndDev-org/openapi-axios that referenced this pull request Apr 24, 2023
@cloudcome
chore: 使用 typescript verbatimModuleSyntax 属性
b22baf4
microsoft/TypeScript#52203
@iisaduan Isabel Duan (iisaduan) mentioned this pull request Apr 25, 2023
"importsNotUsedAsValues": "error" should include type-only imports that are in the same import statement as a value import #51160
Closed
@scotttrinh Scott Trinh (scotttrinh) mentioned this pull request Apr 26, 2023
Update dependencies geldata/gel-js#578
Merged
Mark Napthine (mnapthine) added a commit to action-is-hope/shelley that referenced this pull request May 19, 2023
@mnapthine
fix: replace deprecated entry in tsconfig microsoft/TypeScript#52203
eeb490f
@mnapthine Mark Napthine (mnapthine) mentioned this pull request May 19, 2023
feat(component): tabs component #81 action-is-hope/shelley#87
Merged
@ras0q Ras (ras0q) mentioned this pull request May 22, 2023
upgrade to [email protected] traPtitech/traQ_S-UI#3953
Merged
Ras (ras0q) added a commit to traPtitech/traQ_S-UI that referenced this pull request May 22, 2023
@ras0q
replace importsNotUsedAsValues with verbatimModuleSyntax
7388c76
ref: microsoft/TypeScript#52203
@noahtallen Noah Allen (noahtallen) mentioned this pull request Jul 13, 2023
Update to Typescript 5.1 WordPress/gutenberg#52621
Merged
@ArnaudBarre Arnaud Barré (ArnaudBarre) mentioned this pull request Aug 20, 2023
Error when importing multiple different exports with the same name from different modules (namespaced) vitejs/vite-plugin-react-swc#140
Closed
@chrispcampbell Chris Campbell (chrispcampbell) mentioned this pull request Aug 31, 2023
Upgrade TypeScript, Svelte, and related packages to latest climateinteractive/SDEverywhere#345
Closed
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request Sep 14, 2023
When an enum is imported and used only as a type with "verbatimModuleSyntax" enabled, an error is not being shown #55741
Closed
@kirkwaiblinger Kirk Waiblinger (kirkwaiblinger) mentioned this pull request Dec 31, 2024
Cannot export both types and values from CJS #60852
Open
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@weswigham Wesley Wigham (weswigham) weswigham approved these changes
@sheetalkamat Sheetal Nandi (sheetalkamat) sheetalkamat approved these changes
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) Awaiting requested review from RyanCavanaugh
@sandersn Nathan Shively-Sanders (sandersn) Awaiting requested review from sandersn

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@andrewbranch Andrew Branch (andrewbranch)

Labels

Author: Team For Milestone Bug PRs 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: deprecate importsNotUsedAsValues and preserveValueImports in favor of single flag

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@andrewbranch @johnnyreilly @jakebailey @xfournet @weswigham @sheetalkamat @typescript-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.