You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
However, this means that when combining --noErasingImportedNames with --isolatedModules, types and type-only exports must be imported with import type. Otherwise, single-file transpilation can’t know what import specifiers are safe to preserve:
import{writeFile,WriteFileOptions}from"fs";// ^^^^^^^^^^^^^^^^// TS1444: 'WriteFileOptions' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled.
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 access readFile in the JS would be fs_1.readFile (or another subscript if conflicting), not readFile.
@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 type modifier for ergonomics.
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 import type line). IMO this all adds up to excellent progress towards explicitness and simplicity.
Under --preserveValueImports, import "fs"; or import {} from 'fs'; or ;, which one should we compile it to?
@xiaoxiangmoe Sorry for tagging, but I found your example might not be related in that it has nothing to do with the option/flag --preserveValueImports, which is all about value-having imports as clearly stated in OP. To conclude: An import that is used as a type was destined to be removed when --preserveValueImports was not yet deprecated.
If you found my argument wrong, please let me know. Thanks for your reading.
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.
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
.