I am getting a union type string | number on an object that suppose to have only strings?

Viewed 38

I have the following state logic the issue is that for some reason I am getting a union type string and a number on the Object Key but it has to show only strings.

// State
const inicialState: {
  [key: string]: string;
  type: 'a' |'b' | 'c' | 'd' | 'e' | 'f' | 'g'
  } = {
  names: '',
  data: '',
  type: 'a',
};

const [selected, setSelected] = useState < typeof inicialState > (inicialState);


type ObjectKey = keyof typeof selected;

// type ObjectKey = string | number ??

1 Answers

It seems that keyof can be both a number or a string TS Document I removed the keyof keyword and the ObjectKey has type of inicialState, though you mark the key in that state as a string. I figure that making that a number would be more beneficial.

Related