2

I have a TypeScript Enum that lives within a different namespace to the file I am trying to use it in. The namespace for now unavoidable reasons is long and cumbersome to write

The project is not using modules and does not have a module loader so no imports or exports. My hands are tied here unfortunately. We bundle files manually.

enum Movement { run, walk } 
function Move(movement: App.System.User.Area.Movement) { if (movement === App.System.User.Area.Movement.run) { //... } //.... } 

I am able to use it seems the type keyword to (and I am not sure this is the TypeScript word for it) type def that long namespace away.

type MovementType = App.System.User.Area.Movement; function Move(movement: MovementType) { if (movement === App.System.User.Area.Movement.run) { //... } //.... } 

But I cannot use that type def'd type in the equals comparison in my function above because "MovementType only refers to a type but is being used as a value here" when I try to do:

type MovementType = App.System.User.Area.Movement; function Move(movement: MovementType) { if (movement === MovementType.run) { //... } //.... } 

Is there any way to get around this? Why can't I use it in the conditional statement while I can have it as a parameter? How can I get around my very long namespace?


I am currently using TypeScript 3.1

7
  • An enum is both a set of values and a type. The type keyword creates a kind of alias for the type (that you can use after the : for instance). But indeed I don't think you can use the type alias when you want to access the values of the enum, so the type keyword is not the solution IMO. Commented May 21, 2020 at 13:06
  • You should be able to achieve something next to what you need with import, IMO Commented May 21, 2020 at 13:10
  • try import area = App.System.User.Area;, then use area.Movement instead of App.System.User.Area.Movement. Does this work? Commented May 21, 2020 at 13:13
  • We aren't using modules, again an artifact of the projects age so no imports :( Commented May 21, 2020 at 13:18
  • 1
    In the case it doesn't work, you can have a look at the last section of the docs(see my link in my answer). There is a mention of "ambient namespaces", and how to use declare for this. I don't know how it works, nor do I have the time to experiment properly, but it might worth investigating. Commented May 21, 2020 at 13:29

2 Answers 2

1

As Pac0 mentioned, an enum is both a set of values and a type. If the import solution doesn't work for you, you could try aliasing both the type and value.

type MovementType = App.System.User.Area.Movement; const Movement = App.System.User.Area.Movement; function Move(movement: MovementType) { if (movement === Movement.run) { //... } //.... } 

Example on TypeScript Playground

Sign up to request clarification or add additional context in comments.

1 Comment

Without bundling, this looks like the best option. Thanks
0

The type keyword is indeed only creating an alias for the type that is defined by the enum, it can't work to access the values of the enum (hence, the explicit error messages).

You can achieve something similar to what you want by aliasing the long namespace with import :

import area = App.System.User.Area; function Move(movement: area.Movement) { if (movement === area.Movement.run) { //... } //.... } 

That should at least help a bit.

More info in TypeScript docs.

Example on TypeScript playground

3 Comments

Thank you for your suggestion Pac0, please see my comment above. I have updated the question
ok, If this doesn't work, I'll let my answer stay, as it could be useful for others having a similar problem, and with my sympathy for this not helping you in your case!
That is fine. Yes no luck but your answer might be helpful for others. I should have included the no-modules stuff from the ofset

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.