Typically, each select menu's item has
ID
and
label
. The ID is responsible to communicate with other components, services, or server-side. The label is responsible to display text for users.
This post explains how to manage constants for the menu items which has ID and mapping for its label. It uses TypeScript's
as const
feature which is introduced since v3.4.
In TypeScript, a tuple is an array, but its length and items are fixed. You can define a tuple with
as const
directive on the array literal. (
as const
directive needs TypeScript 3.4+)
Create
colors.ts
and define
colorIDs
tuple as following;
colorIDs[number] means "fields which can be access by number", which are 'green' , 'red', or 'blue' .
So typeof colorIDs[number] becomes the union type 'green' | 'red' | 'blue'.
Because colorLabels has no explicit type, you cannot notice even if you missed to define red 's label.
Let's make sure that colorLabels has a complete label set of all colors! ColorID can help it.
TypeScript gives us Record type to define Key-Value map object. The key is ColorID and the value is string. So colorLabels 's type should be Record<ColorID, string> .
import{Component}from'@angular/core';import{FormControl}from'@angular/forms';import{colorIDs,colorLabels}from'./colors';@Component({selector:'app-root',template:`
<label for="favoriteColor">Select Favorite Color: </label>
<select id="favoriteColor" [formControl]="favoriteColorControl">
<option *ngFor="let id of colorIDs" [ngValue]="id">
{{ colorLabels[id] }}
</option>
</select>
<div>Selected color ID: {{ favoriteColorControl.value }}</div>
exportclassAppComponent{readonlycolorIDs=colorIDs;readonlycolorLabels=colorLabels;readonlyfavoriteColorControl=newFormControl(this.colorIDs[0]);Enter fullscreen modeExit fullscreen mode
As far as I know, there isn't an advantage technically. But I prefer union types because enum is not an ECMAScript's standard syntax.
After transpilation, output code with a tuple and an object is similar to the original TS code, but output code with enum is different from the original TS code a lot.
I think a big value of TypeScript is the mindset; it is a superset of ECMAScript. So I don't want to use enum.
I hear what you're saying! But that's similar to buying a bigger car and never putting anything in the extra space because it wasn't available in your standard car.
I think that the choice of adopting Typescript as a language comes with the acceptance of a build step, the outcome of which is up to the compiler.
Please check the playground link. String enums are just regular objects. You can see in the output of the Playground. typescriptlang.org/play?#code/KYOw...
Thank you! I agree on it is simpler than I posted version.
In other hand, I think the ordering is important in this usecase. Object fields is easily sorted by code editing so it is not safe to keep the ordering.
This is why I want to manage IDs as a tuple. How do you think?
Want to contribute to open source and help make the Angular community stronger?
All the angular code is freely available on GitHub.
You're welcome to jump in!
Once unpublished, all posts by angular will become hidden and only accessible to themselves.
If angular is not suspended, they can still re-publish their posts from their dashboard.