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

后端 未结 6 1811
离开以前
离开以前 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:09

    In my case I needed the properties to be optional, so I created this generic type.

    type PartialRecord = { [P in K]?: T; };
    

    Then use it as such:

    type MyTypes = 'TYPE_A' | 'TYPE_B' | 'TYPE_C';
    
    interface IContent {
        name: string;
        age: number;
    }
    
    interface IExample {
        type: string;
        partials: PartialRecord;
    }
    

    Example

    const example : IExample = {
        type: 'some-type',
        partials: {
            TYPE_A : {
                name: 'name',
                age: 30
            },
            TYPE_C : {
                name: 'another name',
                age: 50
            }
        }
    }
    

提交回复
热议问题