1

I have an enum and i need to define interface for the state using the enum as a key and object as a value: How can I describe the enum a key type

export enum Language {
  CS,
  EN
}

const [userInput, setUserInput] = useState<IUserInput>({
    [Language.EN]: {
      title: '',
      price: '',
      additional_info: '',
      content: ''
    },

interface IUserInput {
    // ?
}
2
  • Did I understand correctly. You want IUserInput to contain properties with key's of type Language and values of Object? Commented Jun 14, 2019 at 9:40
  • yes, sorry I am new to ts so probably my terminology is a bit wrong Commented Jun 14, 2019 at 9:41

1 Answer 1

1

You can either use the computed property syntax:

interface IUserInput {
    [Language.EN]: {
       title: string,
       price: string,
       additional_info: string,
       content: string
    },
}

Or if you want to map over all the keys in the enum you can use the Record mapped type

type IUserInput = Record<Language, {
    title: string,
    price: string,
    additional_info: string,
    content: string
}>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.