Skip to content

Enums cannot be used as a value when imports using the import type declaration. #44998

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

Closed
AnmSaiful opened this issue Jul 13, 2021 · 5 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@AnmSaiful
Copy link

Bug Report

πŸ•— Version Information

TypeScript v4.1.3 Node.js v10.23.1 linux/amd64

⏯ REPL Link

https://replit.com/@AnmSaiful/ts-import-type-enum

πŸ’» Code

// ---- enums.ts ----
export enum Sex {

  Male    = "male",
  Female  = "female",

}

// ---- type.ts ----
export * as Enum from "./enums";

// ---- index.ts ----
import type { Enum } from "./type";

function enumTest(): Enum.Sex {

  return Enum.Sex.Male;

}

console.log( enumTest() );

πŸ™ Actual behavior

It does not allow using Enum from the composed imported type and says:

'Enum' cannot be used as a value because it was imported using 'import type'.

πŸ™‚ Expected behavior

It should allow using Enums from the imported type.

@DanielRosenwasser
Copy link
Member

import type doesn't generate any code, so you'll get a runtime error when evaluating Enum.Sex because Enum won't refer to anything in the output JavaScript.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 13, 2021
@AnmSaiful
Copy link
Author

@DanielRosenwasser I'm using import type to avoid failing ESLint's no-cycle rule because my type imports result from circular dependency. It is normal because the type definition file uses the class file (for the instance type), and class file uses the type definition file.

I can suppress the ESLint error for that line, but should this be practiced?

@AnmSaiful
Copy link
Author

Perhaps we should move this to the feature list instead of bug?

@DanielRosenwasser
Copy link
Member

import type is meant to be erased away entirely - so unfortunately there's no way to make this functionality work that isn't at odds with design decisions we're already set on. At best, there's the discussion of whether we'd want to make exceptions for const enums, which is awaiting more feedback at #40344.

@AnmSaiful
Copy link
Author

Currently, I'm using plain objects as const along with typeof Obj[ keyof typeof Obj] as a workaround. Hopefully, TS will solve the enum issue soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants