-
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
Add --preserveValueImports
#44619
Add --preserveValueImports
#44619
Conversation
--noErasingImportedNames
@typescript-bot pack this |
Heya @andrewbranch, I've started to run the tarball bundle task on this PR at 0b02873. You can monitor the build here. |
@typescript-bot pack this |
Heya @andrewbranch, I've started to run the tarball bundle task on this PR at bc91a25. You can monitor the build here. |
Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
Renamed to |
import { type WriteFileOptions } from "fs"; Under |
My plan was for that to emit |
That's definitely the desired behavior for I assume the whole import would be elided in the case of |
Yeah, my answer was for |
tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts
Outdated
Show resolved
Hide resolved
Thank you for landing this feature in time for TypeScript 4.5! 🎉 I am super excited for the follow-on PR to introduce type-only import identifiers. Probably we would wait for that before enabling this feature, to avoid doubling up on import lines (that would occur via the addition of an |
@xiaoxiangmoe Sorry for tagging, but I found your example might not be related in that it has nothing to do with the option/flag If you found my argument wrong, please let me know. Thanks for your reading. |
Fixes #43393
Successor of #44137
Adds a flag
--noErasingImportedNames
which prevents the elision of unreferenced value-having imports in JS:It does not prevent the elision of imports that resolve to types or type-only exports, because that would create runtime errors:
However, this means that when combining
--noErasingImportedNames
with--isolatedModules
, types and type-only exports must be imported withimport type
. Otherwise, single-file transpilation can’t know what import specifiers are safe to preserve:To make this restriction easier to deal with, a subsequent PR will enable making individual import specifiers type-only:
That PR will also adjust auto-import and organize imports behavior according to these options.
The flag is only valid in es2015+ module emit because downleveling ESM imports to other module systems involves creating a temporary variable with a mangled name and doing property access off that name, so preserving names as written is not possible—the
eval
example wouldn’t work because the way to accessreadFile
in the JS would befs_1.readFile
(or another subscript if conflicting), notreadFile
.