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 |
|
@xiaoxiangmoe that’s an option, but some people (I think reasonably) don't want to have two separate import statements for nearly every module, so we’re planning to add the specifier-level |
|
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
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
--noErasingImportedNameswhich 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
--noErasingImportedNameswith--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
evalexample wouldn’t work because the way to accessreadFilein the JS would befs_1.readFile(or another subscript if conflicting), notreadFile.