1

first of all I think I am missing something about Typescript and Enums although I have done all my studies.

So here we go:

I have lets say the following Enum:

export enum LapTypes {
  'Start' = 'Start',
  'Stop' = 'Start',
  'Manual' = 'Manual',
  'manual' = 'Manual',
  'Autolap' = 'Auto lap',
  'AutoLap' = 'Auto lap',
  'autolap' = 'Auto lap',
  'Distance' = 'Distance',
  'distance' = 'Distance',
  'Location' = 'Location',
  'location' = 'Location',
  'Time' = 'Time',
  'time' = 'Time',
  'HeartRate' = 'Heart Rate',
  'position_start' = 'Position start',
  'position_lap' = 'Position lap',
  'position_waypoint' = 'Position waypoint',
  'position_marked' = 'Position marked',
  'session_end' = 'Session end',
  'fitness_equipment' = 'Fitness equipment',
}

And at a Class of mine I use it like:

export class Lap  {

  public type: LapTypes;

  constructor(type: LapTypes) {
    this.type = type;
  }

}

When I create a new lap as so:

const lap = new Lap(LapTypes.AutoLap);

All is fine.

Then again if I do this:

const lapType = 'AutoLap';

this new Lap(LapTypes[lapType]) works just fine

However, since I want a dynamic Laptype I am trying to do the following:

const lapType: string = someJSON['Type'];

but when I try to create a new Lap

new Lap(LapTypes[lapType])

I get:

element implicitly has an 'any' type because index expression is not of type 'number'

I am sure I am missing something fundamendal here and need to restudy my typescipt.

I would like some help on what I am doing wrong and where to look to broaden my knowledge.

9
  • Is the laptype from the JSON what you expect it to be or is it some other value? And also the way you use the enum, to me it seems that a map would be more appropriate. Commented May 13, 2018 at 11:21
  • @MarkoTaht its a sting inside the JSON and I am getting this in compile type. In sort it wont let me compile. I am using the latest TS. I could make it a map or a const with index but I would like to narrow down the cases with an Enum as of API first approach. Commented May 13, 2018 at 11:28
  • stackoverflow.com/questions/36316326/… Here someone had the same issue. What you have to do is LapTypes[<any>lapType]. Commented May 13, 2018 at 11:33
  • Does something like const lapType = <keyof typeof LapTypes>someJSON['Type'] work? Commented May 13, 2018 at 11:37
  • @MarkoTaht that does not work Commented May 13, 2018 at 11:47

2 Answers 2

2

Since enum member names are specific strings and not just random strings, string isn't a proper type for enum key.

If someJSON.Type is any, it can be:

const lapType: keyof typeof LapTypes = someJSON['Type'];
new Lap(LapTypes[lapType]);

If someJSON.Type has been already typed as string, it can be:

const lapType = <keyof typeof LapTypes>someJSON['Type'];
new Lap(LapTypes[lapType]);

Considering that someJSON is untyped or loosely typed, it should be properly typed as early as possible. keyof typeof LapTypes type should preferably be specified for Type property at someJSON variable definition.

Sign up to request clarification or add additional context in comments.

1 Comment

Loud and clear and what I was starting to suspect but did not really know! Thanks for this
1

simply use: new Lap(LapTypes[lapType as LapTypes])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.