1

I want to use my enum to make an interface.

Just because one should change and the other one also needs to be changed. I want to change the code more closely.

The enum's key is the same Interface's Key.

export enum SmsReplaceKeyEnum {
  STAY_DATE = "%STAYDATE%",
  STAY_DATE_YMD = "$STAYDATEYMD%",
  ROOMTYPE_N_COUNT = "%ROOMTYPENCOUNT%",
  BOOKERNAME = "%BOOKER%",
  TOTAL_PRICE = "%TOTALPRICE%"
}

interface IPareser {
  STAY_DATE: string;
  STAY_DATE_YMD: string;
  ROOMTYPE_N_COUNT: string;
  BOOKERNAME: string;
  TOTAL_PRICE: string;
}

I also have this one

export const SmsReplaceKeyEnumKeys = [
  "STAY_DATE",
  "STAY_DATE_YMD",
  "ROOMTYPE_N_COUNT",
  "BOOKERNAME",
  "TOTAL_PRICE"
];

I tried

interface IPareser {
  [key in SmsReplaceKeyEnumKeys]: string;
}

interface IPareser {
  [key keyof SmsReplaceKeyEnumKeys]: string;
}

The final goal is to create an object's type with all the keys in the list.

1 Answer 1

4

You can't use mapped types syntax inside interface declaration in TS (now, up to version 3.5). You can make the desired type directly from your enum like this:

type Parser = {
  [K in keyof typeof SmsReplaceKeyEnum]: string;
}
Sign up to request clarification or add additional context in comments.

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.