Search code examples
typescriptinterfacenormalization

TypeScript: An index signature parameter must be a 'string' or 'number' when trying to use string | number


I'm attempting to create a function to normalize my arrays and it's expecting an output object that is structured like this:

{
  allIds: [1],
  byId: {
    1: {...}
  }
}

OR

{
  allIds: ['1'],
  byId: {
    '1': {...}
  }
}

I'm trying to create an interface called IOutput to cater for this.

I've tried this:

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: number | string]: any
  }
}

But it gives me the following error

An index signature parameter type must be 'string' or 'number'. ts(1023)

It seems to work when I do this:

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: number]: any
  }
}

OR

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: string]: any
  }
}

But that's not what I'm trying to accomplish. I've also tried this and it gives me the same error:

type StringOrNumber = string | number

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: StringOrNumber ]: any
  }
}

How can I accomplish what I'm trying to do?


Solution

  • Now the limitation to write indexes has been removed. An index signature parameter can be union of number and string.

    type StringMap = { [key: string | number]: unknown };
    
    function createStringPair(property: keyof StringMap, value: number): StringMap {
      return { [property]: value };
    }
    console.log(createStringPair(5, 10));
    console.log(createStringPair("sum", 10));
    
    
    [LOG]: {
      "5": 10
    } 
    [LOG]: {
      "sum": 10
    } 
    

    Cheers, hope it helps.