0

I have an interface that looks like that:

interface Person {
    name: string;
    age: number;
    group: Options
}

and an enum that looks like this:

export enum Options {
    ONE = 'one',
    TWO = 'two',
    THREE = 'three',
}

I want to create a new object where the key will be one of the Options and the value will be Person.

I tried something like that:

type PersonMapped = {
    [key in Options]: Person; // error on the in keyword
}

But I get an error that says: parsing error: Unexpected token, expected "]". What's wrong with this code?

Edit 1: Here's the code example that I try to use:

export enum Group {
    ONE = 1,
    TWO = 2,
    THREE = 3
}

export type Mappable = {
    [key in Group]: string
}

Here's the error from the ide:

enter image description here

Edit 2: It seems that the problem is with my editor settings. I'm using vscode version - 1.59.1. I also have eslint extension installed with version - 2.1.23.

Keselme
  • 3,779
  • 7
  • 36
  • 68
  • 2
    [I can't reproduce your error](https://tsplay.dev/W4pd4W). Please consider providing a [mcve] either in the form of plaintext that can be dropped into any standalone IDE to demonstrate the issue, or as a link to a properly configured web IDE project which demonstrates the issue. Right now my guess is you're trying to write TypeScript code in a JavaScript file but that's just a guess – jcalz Sep 02 '21 at 16:09
  • Please take a look at https://stackoverflow.com/questions/53508709/use-an-enum-to-type-an-index-signature-of-an-interface-field – captain-yossarian from Ukraine Sep 02 '21 at 16:21
  • @jcalz, I rechecked that I am actually in TS file. I added a simple example in my question. – Keselme Sep 03 '21 at 20:02
  • 1
    So your problem seems to be with your editor or project configuration and not with TypeScript. As I said before, [your TypeScript code is fine](https://tsplay.dev/mqvgrW); I see from the image that eslint is complaining, so you might want to tag this question with eslint and whatever editor you're using, and give more details about your project/editor configuration. – jcalz Sep 03 '21 at 20:10
  • @jcalz, so do I need to add other tags to this question, or should I ask it again with new tags? – Keselme Sep 03 '21 at 20:12
  • I would add tags instead of re-asking if I were you, but that's your choice. Good luck! – jcalz Sep 03 '21 at 20:17

0 Answers0