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
enumis both a set of values and a type. Thetypekeyword 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 thetypekeyword is not the solution IMO.import, IMOimport area = App.System.User.Area;, then usearea.Movementinstead ofApp.System.User.Area.Movement. Does this work?declarefor this. I don't know how it works, nor do I have the time to experiment properly, but it might worth investigating.