Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enum to determine which item to use from an import in TypeScript?

Tags:

typescript

Overview

We have a module that exports objects from other files.

For simplicity sake, let's call this Vehicle. It exports Car, Boat, and Plane.

We have an enum that corresponds to these vehicles, let's say the following:

enum Vehicles {
    Car,
    Boat,
    Plane,
}

We are looking to use the enum to specify which imported object we should then use.


Question

We're aware that we can do a switch/case, but this becomes unnecessarily long as our enum grows. Some of our classes export dozens of variations of objects that we then use elsewhere (we are unable to use types/interfaces to simplify).

switch(vehicle) {
    case Vehicles.Car: {
        return Car;
    }
    case Vehicles.Boat: {
        return Boat;
    }
    case Vehicles.Plane: {
        return Plane;
    }
}

More info

We are using TypeScript with Svelte. The import is a package (e.g. Google Charts [charts], fortawesome [icons], etc.). We are looking to create a sort of wrapper to easily initialize specific components.

Example:

<script lang="ts">
    import { a, b, c } from x
    const y = () => {
        // logic here
    }
</script>

<y/>

This question seems to be TypeScript-specific, so I've purposely left out svelte tag from my question

like image 278
ctwheels Avatar asked Apr 02 '21 03:04

ctwheels


People also ask

How do I use enum values in TypeScript?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.

Why do we use enum in TypeScript?

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.

Can you export an enum TypeScript?

TypeScript uses the concept of modules, in the same way that JavaScript does. In order to be able to import an enum from a different file, it has to be exported using a named or default export.

What is enum and how do you use it in angular?

Enums let you avoid using strings and having to remember what string values you used. They also allow you to lock down the values a property can be, so that a user can't just submit any value they want. So if you aren't using them, start using them, and if you are already using them, use them more, use them to a fault.


Video Answer


1 Answers

So I tried some things, here you can see them:


What I originally wanted to do

I tried to assign the classes to the enum properties, but it didn't work. How I imagined it is like this:

enum Vehicle {
    Car = Car,
    Boat = Boat,
    Plane = Plane,
}

But TypeScript gives me an error. You can try it here. Why did I think about that? Because you can assign value to enum properties like this:

enum Char {
    A = 65,
    B = 66,
    C = 67,
    D = 68,
    // ...
}

But as you can see, it doesn't work in your case.


A possible solution

You could just forget using an enum. There is a special feature in TypeScript, as const. It makes an array or object literal constant. That would look somehow like this:

const Vehicle = {
    Car,
    Boat,
    Plane
} as const;

Why don't I use a value? That is called property shorting (or something like that). When you have a variable or some named thing and want to use it with the same property name, you don't have to specify one. Here is the example using the as const version.


What when you want to get the class by an index?

You can use Object.keys to get an array of keys. It look somehow like this:

const index = 1;
const keys = Object.keys(Vehicle);
const vehicle = Vehicle[keys[index]];

I'm not going to add a link here because it is not really important.

like image 61
blaumeise20 Avatar answered Nov 15 '22 08:11

blaumeise20