-
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
"importsNotUsedAsValues": "error"
should include type-only imports that are in the same import statement as a value import
#51160
Comments
importsNotUsedAsValues
ignores type-only imports that are in the same import statement as a value.importsNotUsedAsValues
ignores type-only imports that are in the same import statement as a value import
importsNotUsedAsValues
ignores type-only imports that are in the same import statement as a value import"importsNotUsedAsValues": "error"
should include type-only imports that are in the same import statement as a value import
@andrewbranch thoughts? |
Agreed; this is a bug. Otherwise, though, this is working as intended. We made some terminology simplifications in the docs that I now regret, so maybe a more precise wording will clear things up:
That is, the error occurs whenever an import declaration will be emitted to JS unnecessarily. By this logic, you can see why it doesn’t care how many types are also imported in the same import declaration, as long as there is at least one identifier that will stick around in the JS emit: // ts
import { Type1, Type2, Type3 } from "./types";
// is an error because it would emit:
import {} from "./types";
// but
import { Type1, Type2, Type3, someValue } from "./types";
someValue;
// is ok because it would emit:
import { someValue } from "./types";
someValue; This should also explain:
That’s not what TypeScript was caring about. What the option cares about is that the entire import declaration suddenly became unused in the JS emit. You made an edit that fundamentally altered the runtime module dependency graph and you either need to signal that you intended to elide the runtime dependency or explicitly depend on it with a side-effect import of
Many people have said this, but it just wasn’t a goal of |
Thanks!
If the current functionality won't change, I think this would definitely be a significant improvement and I'd love to see such a wording change. That said, I still think it would be confusing that "(error on) imports not used as values" doesn't do what it says on the tin, for most people's colloquial understanding of "an import" as an individual named import rather than the an entire import statement (which seems to be so widespread that it was incorrectly used in the docs, as we are discussing).
Yeah, unfortunately I understand the details all too well. 😭
I would definitely be glad to see this. In theory I wouldn't care about this so much, but it would help a lot with codebase maintenance in practice, especially if you want code to be understood consistently by third-party tools in the ecosystem. |
I second this; I’ve wanted that for quite a while. Since |
|
Bug Report
🔎 Search Terms
type-only type modifier import importsNotUsedAsValues tsconfig.json
🕗 Version & Regression Information
Relevant since TypeScript 4.5: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#type-modifiers-on-import-names
⏯ Playground Link
N/A, I can't see a way to use the playground or workbench to show an issue with multiple files, and/or a specific
tsconfig.json
setting.💻 Code
🙁 Actual behavior
TypeScript does not error on:
import { myValue, MyType } from "./b";
Further, edit
a.ts
as in VSCode follows and "Organize imports":This results in
ts(1371)
because the import is rewritten intoimport { MyType } from "./b";
... which now violatestsconfig.json
. This is arguably "safe" behaviour in case you want to preserve any side effects of importingmyValue
as a type, but it's not a great code editing experience. Even though"importsNotUsedAsValues": "error"
has been set this whole time, TypeScript "suddenly starts caring" thatMyType
is actually only used as a type, whereas it did not do so before "Organize Imports". This makes sense if you learn the details of TypeScript, but I think it would be less confusing if TypeScript "started caring earlier".🙂 Expected behavior
import { myValue, type MyType } from "./b";
import type { MyType } from "./b";
on a separate line.Further, when refactoring it is valuable to be able to tell which names are "actually" value vs. type imports — in particular, to be able to distinguish the following at a glance. I'd like to rely on
importsNotUsedAsValues
to clearly indicate every type import across the codebase. But TypeScript currently doesn't care about the difference between the following:I understand that the documentation of
importsNotUsedAsValues
states that it is "to explicitly create an import statement which should never be emitted into JavaScript". So from the original motivation ofimportsNotUsedAsValues
(i.e. for compilers working on a particular snapshot of the code rather than humans maintaining code over time), this isn't a big deal. However, just above, the documentation clearly also states:Since an entire import statement cannot be used as a type, I believe the only valid interpretation of the excerpt is that "value import" refers to an individual named import in an import statement. This means that
MyType
inimport { myValue, MyType } from "./b";
is a "value import only used as a type" in and should error. I've been confused about this inconsistency for a long time, and am just now filing an issue because I've finally dissected the mismatch between TypeScript's documentation and behaviour.If this fix for
"importsNotUsedAsValues": "error"
is not backwards-compatible enough at this point, I'd love to see at least a new value to enforce thetype
modifier on imported names not used as values, such as"importsNotUsedAsValues": "error-per-name"
.Either way, for the sake of consistency, code clarity, and refactoring I think there should be a way for
importsNotUsedAsValues
to error like this (independent ofisolatedModules
):The text was updated successfully, but these errors were encountered: