By callmetwan


2020-03-26 02:52:37 8 Comments

Looking to type the possible values in an array as those of an enum:

enum MyEnum {
    FirstRole: "FIRST"
    SecondRole: "SECOND"
}

type MyType {
   roles: MyEnum[] // I want to enforce this array to only strings of FIRST or SECOND as depicted in the enum i.e. ["FIRST", "SECOND"]

2 comments

@iamhuynq 2020-03-26 03:25:12

Simply like this

    enum MyEnum {
        FirstRole = 'FIRST',
        SecondRole = 'SECOND',
    }
    type role = 'FIRST' | 'SECOND';
    const role: MyEnum[] | role[] = [MyEnum.FirstRole, 'FIRST'];

@Chirag Rupani 2020-03-26 03:24:11

You can simply declare array using custom type like below:

type Role =  "First" | "Second";
let roles: Array<Role> = [ "First"];

The roles would allow only First or Second strings.

@callmetwan 2020-03-28 03:18:35

The point of an enum is to be able to reference a value without having to use it as a literal. Using this method we are using string literals, which means you have to update the type Role every time the enum changes, which defeats the purpose of using it. It seems typescript doesn't support this at all.

@Chirag Rupani 2020-04-01 03:36:27

You don't have to use both enum and role type. If you need to you can use it like below: enum roleType { "First", "Second" }; type Role = roleType.First | roleType.Second; let roles: Array<Role> = [roleType.First];

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] dynamically access enum in typescript by key

  • 2018-05-18 18:27:40
  • annepic
  • 4357 View
  • 3 Score
  • 3 Answer
  • Tags:   typescript enums

7 Answered Questions

[SOLVED] TypeScript: Interfaces vs Types

  • 2016-05-15 01:53:52
  • user6101582
  • 113102 View
  • 643 Score
  • 7 Answer
  • Tags:   typescript

21 Answered Questions

[SOLVED] How do you explicitly set a new property on `window` in TypeScript?

  • 2012-10-03 13:01:15
  • joshuapoehls
  • 307607 View
  • 572 Score
  • 21 Answer
  • Tags:   typescript

7 Answered Questions

[SOLVED] Are strongly-typed functions as parameters possible in TypeScript?

  • 2013-02-01 02:56:27
  • vcsjones
  • 263338 View
  • 533 Score
  • 7 Answer
  • Tags:   typescript

1 Answered Questions

How to define the type "any string enum" in typescript?

  • 2019-12-22 19:38:34
  • distante
  • 43 View
  • 0 Score
  • 1 Answer
  • Tags:   typescript

16 Answered Questions

[SOLVED] TypeScript Converting a String to a number

  • 2013-02-02 23:34:18
  • Paul0515
  • 880120 View
  • 816 Score
  • 16 Answer
  • Tags:   typescript

13 Answered Questions

[SOLVED] TypeScript and field initializers

  • 2013-01-03 15:36:45
  • Nickon
  • 198944 View
  • 229 Score
  • 13 Answer
  • Tags:   typescript

8 Answered Questions

[SOLVED] get and set in TypeScript

  • 2012-10-10 19:52:14
  • MuriloKunze
  • 546496 View
  • 621 Score
  • 8 Answer
  • Tags:   typescript

3 Answered Questions

[SOLVED] Typescript derive union type from tuple/array values

  • 2017-07-22 06:54:51
  • WHITECOLOR
  • 11359 View
  • 61 Score
  • 3 Answer
  • Tags:   typescript

1 Answered Questions

[SOLVED] Typescript enum value as array parameter

  • 2017-10-25 21:02:05
  • Karl Johan Vallner
  • 1546 View
  • 2 Score
  • 1 Answer
  • Tags:   angular typescript

Sponsored Content