Skip to content
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

When an enum is imported and used only as a type with "verbatimModuleSyntax" enabled, an error is not being shown #55741

Closed
lsalazarm99 opened this issue Sep 14, 2023 · 18 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@lsalazarm99
Copy link

πŸ”Ž Search Terms

"verbatimModuleSyntax", "enum"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "verbatimModuleSyntax" (there wasn't any) and "enums".

⏯ Playground Link

https://www.typescriptlang.org/dev/bug-workbench/?verbatimModuleSyntax=true&ts=5.0.4#code/PTAEAEDcFMCcCMCGAXAlgWwLIHsAmBXAG2gGUBPAO2UQA8AuUZWfaAKFZAgDNViLF00Bj2IBaAO6pkAC1HQaAB2yxkAOmQBnVvKUrQ0CvnSgA6stwbQAb1ag7oAIKgAvKABEDtwBpb9gEIu7n7erAC+7DrKyIxkCtCm5oEaTKgUAOYA3Oyc4CIGAkKgqbjy6loYutFWCbAWXjW4oKGgXLDYxm6qwHkSUrKRKm7sxNGIDGa1WSOgiBRkE7jj5hoZQA

πŸ’» Code

// @verbatimModuleSyntax: true

// @filename: file-with-export.ts
export enum Words {
    A = "A",
    B = "B",
}

export type Word = string;

// @filename: index.ts
import { Words, Word } from "./file-with-export"

let a: Word;
let anyWord: Words;

πŸ™ Actual behavior

The import of the Words enum doesn't show any errors.

πŸ™‚ Expected behavior

The import of the Words enum should show a 1484 error.

Additional information about the issue

The mentioned import should show an error because it's being used only as a type and the "verbatimModuleSyntax" flag is enabled.

@fatcerberus
Copy link

It’s being used only as a type, but it’s a valid runtime import, so I don’t see why this would be an error.

@lsalazarm99
Copy link
Author

πŸ€” I understand your point, and I think you are right. In that case, how can I get the effects of the "importsNotUsedAsValues" flag when marked as "error"? Since it's going to be deprecated in favor of "verbatimModuleSyntax", I would expect the latter to handle its functionality.

@MartinJohns
Copy link
Contributor

Since it's going to be deprecated

Where did you read that?

@lsalazarm99
Copy link
Author

lsalazarm99 commented Sep 14, 2023

It's in the documentation of the rule, here. Actually, it's already deprecated. πŸ˜…

@lsalazarm99
Copy link
Author

Also "tsc" says it when I use the flag:

error TS5101: Option 'importsNotUsedAsValues' is deprecated and will stop functioning in TypeScript 5.5. Specify compilerOption '"ignoreDeprecations": "5.0"' to silence this error.

I can't use both flags at the same time neither because "tsc" shows this error:

error TS5104: Option 'importsNotUsedAsValues' is redundant and cannot be specified with option 'verbatimModuleSyntax'.

@MartinJohns
Copy link
Contributor

Thank you. I completely skipped that information when reading the documentation.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Sep 14, 2023
@RyanCavanaugh
Copy link
Member

There isn't a new configuration that corresponds to the old behavior of erroring here, since no "problem" per se has occurred (only a style violation). #52203 (comment)

@jakebailey
Copy link
Member

If the output JS had this import removed, that'd be a bug, but the flag doesn't tell you that you could switch an import to a type import because it's only used in type positions. verbatimModuleSyntax means "only type annotated imports are removed" and that's it.

If you want to be told to use a type import when possible, you need: https://typescript-eslint.io/rules/no-import-type-side-effects/

@fatcerberus
Copy link

verbatimModuleSyntax means "only type annotated imports are removed" and that's it.

Hmm, shouldn’t the unannotated import of Word (not Words) be an error then, since that’s a pure type and thus can’t be imported at runtime?

@RyanCavanaugh
Copy link
Member

Hmm, shouldn’t the unannotated import of Word (not Words) be an error then

It is

image

@jakebailey
Copy link
Member

It is:

image

@RyanCavanaugh
Copy link
Member

My cropping is better

@jakebailey
Copy link
Member

I had a colon

@MartinJohns
Copy link
Contributor

@jakebailey I hope you still have a colon.

@fatcerberus
Copy link

It is

Ah okay, good. I wasn’t sure because the OP only mentioned the β€œmissing” error on Words so I just assumed the rest of the repro code was legal. I probably should have clicked the playground link before commenting πŸ˜…

@lsalazarm99
Copy link
Author

lsalazarm99 commented Sep 14, 2023

Got it! Thank you all for your help! I think the new flag is still a bit confusing. I was expecting it to be able to do the same things that the deprecated flag does with the "error" option.

As @jakebailey said, @typescript-eslint has the situation covered. By the way, I think you were talking about the @typescript-eslint/consistent-type-imports rule, since it is the one that makes the expected error be shown.

@jakebailey
Copy link
Member

Some combo of those rules, sure. You do probably want the rule I mentioned if you want to be warned that import { type A } from "..." will turn into import {} from "..." at runtime.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

6 participants