Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enum as index key type in typescript?

Consider following example.

enum DialogType {     Options,     Help }  class Dialog {      test() : string {         return "";     } }  class Greeter {      openDialogs: { [key in DialogType]: Dialog | undefined } = {         0: undefined,         1: undefined     };      getDialog(t: DialogType) {         return this.openDialogs[t];     } }  const greeter = new Greeter(); const d = greeter.getDialog(DialogType.Help); if (d) document.write(d.test()); 

Also in playground

There are 3 issues/questions with it:

  1. Why I cannot omit properties in my initializer literal, even though I declare properties as '| undefined'
  2. Why I cannot use 'DialogType.Options' as type key, and have to use hardcoded number instead?
  3. Why do I have to use 'key in DialogType' instead of 'key: DialogType'? (Or can I? )
like image 852
ironic Avatar asked Oct 08 '18 10:10

ironic


People also ask

Can I use enum as type in TypeScript?

Enums or enumerations are a new data type supported in TypeScript. Most object-oriented languages like Java and C# use enums. This is now available in TypeScript too. In simple words, enums allow us to declare a set of named constants i.e. a collection of related values that can be numeric or string values.

Can I use TypeScript enum in JavaScript?

Enums are a feature added to JavaScript in TypeScript which makes it easier to handle named sets of constants. By default an enum is number based, starting at zero, and each option is assigned an increment by one. This is useful when the value is not important.

Is enum a type or value 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.


1 Answers

  1. |undefined does not make a property optional, just means it can be undefined, there is a proposal to make |undefined members optional but currently it's not implemented. You need to use ? after ] to make all properties optional

    { [key in DialogType]?: Dialog } 
  2. You can use the dialog enum values as keys, but they need to be computed properties:

    let openDialogs: { [key in DialogType]?: Dialog } = {     [DialogType.Options]: undefined, }; 
  3. { [key: number or string]: Dialog } is an index signature. Index signatures are restricted to only number or string as the key type (not even a union of the two will work). So if you use an index signature you can index by any number or string (we can't restrict to only DialogType keys). The concept you are using here is called mapped types. Mapped types basically generate a new type based on a union of keys (in this case the members of DialogType enum) and a set of mapping rules. The type we created above is basically equivalent to:

    let o: { [DialogType.Help]?: Dialog; [DialogType.Options]?: Dialog; } 
like image 91
Titian Cernicova-Dragomir Avatar answered Sep 19 '22 01:09

Titian Cernicova-Dragomir