An index signature parameter type cannot be a union type. Consider using a mapped object type instead

后端 未结 6 1813
离开以前
离开以前 2020-12-03 16:57

I\'m trying to use the following pattern:

enum Option {
  ONE = \'one\',
  TWO = \'two\',
  THREE = \'three\'
}

interface OptionRequirement {
  someBool: bo         


        
6条回答
  •  感动是毒
    2020-12-03 17:24

    Instead of using an interface, use a mapped object type

    enum Option {
      ONE = 'one',
      TWO = 'two',
      THREE = 'three'
    }
    
    type OptionKeys = keyof typeof Option;
    
    interface OptionRequirement {
      someBool: boolean;
      someString: string;
    }
    
    type OptionRequirements = {                 // note type, not interface
      [key in OptionKeys]: OptionRequirement;   // key in
    }
    

提交回复
热议问题