-
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
Auto import adds types to existing value imports under --importsNotUsedAsValues=error #39432
Comments
This is expected behavior. A type-only import is unnecessary if there is already a regular import from the same module. The point of type-only imports is to eliminate runtime dependencies between modules. You already have an unavoidable runtime dependency on that module, so adding a type-only import has no effect on the dependency graph. Auto-imports will only give you a type-only import if a regular import would cause an error under |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@andrewbranch
then I auto-import some types:
and then some other developer will refactor this module so
That could be easily replaced by
So if I use only auto-imports, at some point I can have unnecessary runtime dependencies only due to this bug. If it works properly, it will add only type imports, so in case if any values will be removed from the module we will have no runtime dependency. In the way it works right now, it helps to get rid of dependencies only in some cases, in other users would need to manually go through all the dependencies and check them manually and it kills the purpose of that flag. Please reopen the issue |
^ this is an error with
No;
No; I would also be remiss not to mention the fact that |
My bad, I actually had |
I strongly disagree with this assessment. You don’t have to move your import specifiers around. There is no reason to do that other than aesthetics. My personal coding philosophy is that focusing on code aesthetic is a job for machines, not humans. TypeScript’s job is to add another layer of meaning to your code, and how you collate your import specifiers, in the absence of an error issued by |
There are some reasons to move types to type imports.
|
I don’t really understand why “number of module dependencies” is relevant, as the total runtime size of the dependency graph from a single module could be anywhere from zero bytes to as much memory as the runtime can allocate to code compilation. There are other tools that let you inspect the size of that graph, which seems infinitely more relevant. But again, I have to emphasize that with
Babel does this, yes. Deno uses the TypeScript compiler. If this is a limitation of future tools, it sounds like something those tools should address. If there is a lot of demand for enforcement of this kind of rule in the future, stemming from reasonable causes other than aesthetics, it’s something we could consider. But you’re only the second person to ask for something like this for the purposes of external tools, and it sounds like your concern is still hypothetical at this point. If we were to add it now, I predict it would mostly serve to fuel meaningless stylistic dogmatism, making everyone less productive. |
Over at Svelte we have the need to strictly separate type and value imports. The use case is preprocessing the script contents of a Svelte file. Given this input: <script lang="ts">
import { value, valueOnlyUsedInTemplate } from './somewhere';
import type { aType } from './somewhere';
const foo: aType = value;
</script>
{foo} {valueOnlyUsedInTemplate} The preprocessor invokes import { value, valueOnlyUsedInTemplate } from './somewhere';
import type { aType } from './somewhere';
const foo: aType = value; Given the current mechanics of TS transpilation, both
This is probably what the OP wanted, which would be enabled by #43393 |
TypeScript Version: 4.0.0-dev.20200705
Search Terms: auto import type
Code
Expected behavior:
When auto-importing a type in a type-only place, with
"importsNotUsedAsValues": "error"
, it should always add a type importActual behavior:
If there is an existing import for where the type is coming from, it'll add it to that import, instead of creating a new type-only import
Here's a video of the bug:
Playground Link: Not applicable
Related Issues: Didn't see any
The text was updated successfully, but these errors were encountered: