Igor Pavlenko
Igor Pavlenko

Reputation: 717

Interface for enum with nested types

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 {
    // ?
}

Upvotes: 1

Views: 277

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249766

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
}>

Upvotes: 1

Related Questions