-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
request: add { importsNotUsedAsValues: preserve-values } to compilerOptions #43687
Comments
I would be open to a mode where
Would that solve the svelte problem? |
Note: my counter-suggestion is essentially #43393 |
nah, we want to mix types and values like
|
But you don’t need to mix types and values in your imports, right? In this mode of checking, transpilers should never need to guard against some export or file not existing because it was just a type. |
no, but we want to ... and we want to avoid the workaround that
so that to typescript, imports appear to be used, so they are not removed in import eliding that workaround is expensive cos it involves double-parsing the html markup but generally, this would be a "nice to have" feature for the ts compiler |
I see. Well, I think the answer to this suggestion is probably no. What I’m proposing fixing is
If we implemented my proposal, it would have two consequences:
That seems much better than the status quo, because it would improve transpilation performance and guard against a pitfall that seems very easy to fall into. |
I’m going to reopen #43393 and move the conversation to there. |
Suggestion
im looking for a
{ importsNotUsedAsValues: 'preserve-values' }
compiler-option, where1. all type-imports are removed, including "empty imports" like
import "./some"
andimport "./where.d.ts"
2. unused imports are preserved, including maybe-type-imports
this would make it much easier to compile code-fragments in isolation
where ts has no access to other files, and no access to the fragment's context
1. type-imports should be fully removed
2. unused imports should be preserved
to support the infamous
export { SomeThing } from './some-where'
casewhere ts.transpileModule cannot tell whether
SomeThing
is a type or value:use a fault-tolerant module loader, like
this is needed for development mode, where tree-shaking is disabled
so imports from type files would give fatal errors
deprecated: transpile type exports to "dummy exports"
edit: this is deprecated, since it breaks with external modules
external modules are not compiled in the current build pipeline
so there is no way we can generate "dummy exports" for all maybe-type-imports
in case
SomeThing
is a typets.transpileModule would have to generate "dummy exports"
like
export const SomeThing = null;
insome-where.js
... to avoid the runtime error
SomeThing is not exported by some-where
or
no such file: some-where
adding such dummy exports should be safe
since collisions between value IDs and type IDs are not possible in ts
and IDs are (usually) scoped to modules
this solution is "clean", cos the added dead-code is removed later in the build pipeline (tree-shaking in production mode)
compiler call (playground on codesandbox)
🔍 Search Terms
single file compilation
ts.transpileModule
remove only type imports
keep unused value imports
generate dummy exports for types
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
📃 Motivating Example
💻 Use Cases
references
single file transpile: sveltejs/svelte-preprocess#318 (comment)
dummy exports: https://stackoverflow.com/a/52260432/10440128
api docs: https://www.typescriptlang.org/tsconfig#importsNotUsedAsValues
The text was updated successfully, but these errors were encountered: