Description
Is your feature request related to a problem? Please describe.
Currently, TypeScript is processed on a per-file-basis. Because the script tag is only part of the truth, a transformer was written to re-add imports which to TypeScript look unused but are actually used in the template. This means that people need to be very careful about how to write their imports, which can be especially cumbersome if a type import is from the same file as a real import.
<script lang="ts">
impor type { Type } from './foo';
impor { Bar } from './foo';
</script>
Since TypeScript itself does not do that strict autocompletion separation (and they don't plan to change it, see this issue), people have to do it by hand.
So what would be ideal is to
- either somehow tell TypeScript which values are used so it does not throw them out, which would make the transformer unnecessary
- or write a more sophisticated transformer which traverses the AST and throws out all types from the imports (similar to Fix/remove type imports typescript #164, only without relying on a program if possible)
Describe the solution you'd like
Ideally, I don't have to be that strict with seperating type and value imports. I currently see two ways to get there.
Solution 1: The Svelte-AST-compile-route
This solution is similar to the one currently used in eslint-plugin-svelte3
. Some logic of #155 could maybe be reused.
- get the whole Svelte file, blank style content
- transpile script tags' TS to JS using the current transform which preserves all imports
- do a svelte.compile which will return a list of variables with the info where they are used (module, template, etc)
- append all variables to the bottom of the TS code so TS sees this "fake usage" and does not throw out imports that are only used in the template
- transpile TS to JS without the "keep all imports" transformer
Advantages:
- probably the most correct and safest varaint
Disadvantages:
- Now TS needs the whole markup and would technically be a markup, not a script transformer. That's likely a breaking change
- It wouldn't work if people use markup that needs preprocessing, or that would need to happen before the script transformation
Solution 2: Walk TS AST to find type only references
This solution would be similar to #164 . Basically, walk the AST and find all references of all imports, and filter those out who are type-only. Walking the AST without advanced capabilities like a type checker / TS program is likely tedious, but probably more robust.
Advantages:
- no breaking change, only needs script contents
Disadvantages:
- maybe not possible without expensive type checker (although solution 1 would also be slower)
- there may be edge cases where this does not work: If someone imports a type in module-script but uses it as a type in the instance-script. The logic would be "oh this is unused and not otherwise used as a type, preserve it" which would be wrong.
How important is this feature to you?
Important in order to boost TypeScript development productivity.