10

I am trying the keep route url's in an enum like below.

export enum RouteUrls {
HOME = '/home',
DASHBOARD = `${HOME}/dashboard` // error here 
}

In the above line I am getting error as "Computed values are not permitted in an enum with string valued members.", is there any other way to achieve the same using Enum in TypeScript.

Gopi
  • 201
  • 1
  • 2
  • 10
  • Does this answer your question? [Can I have an Enum reference a text string?](https://stackoverflow.com/questions/25820171/can-i-have-an-enum-reference-a-text-string) – milt_on Oct 25 '21 at 07:16
  • 2
    Miles, that doesn't answer this question – Cerbrus Oct 25 '21 at 07:18
  • Not with enum, but you could use regular object https://www.typescriptlang.org/play?#code/MYewdgzgLgBAEgeQLIFEYF4YHID0ALEAWwFMsAoYgDwAcQAnWUSWAJRAFcpiBVOgGwgYYAbzIx4yFABoxMACIBBAMpwAQggUs5ALhgADACTDEqAL44AJgEMIeAEYgrdC3rKmYNmE2hA – Aleksey L. Oct 25 '21 at 07:18
  • What benefit do you get from an `enum` that you can't get from a [`const` assertion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions)? – Robby Cornelissen Oct 25 '21 at 07:19
  • Computed members are allowed only in numeric enums – Aleksey L. Oct 25 '21 at 07:25

1 Answers1

11

For now only numeric enums can have computed members. What you are trying to do is to have string enum with computed members, which is not permitted in TypeScript. I found that there is an open issue about this topic in official Microsoft TypeScript git repository. Maybe this feature will be implemented in the future versions.
URL to issue on git: https://github.com/microsoft/TypeScript/issues/40793

shemekh
  • 426
  • 5
  • 14