1

Heres the situation: suppose I have an Array with the following structure:

const myArray = [
  { label: 'A title', value: 'A'},
  { label: 'B title', value: 'B'},
  { label: 'C title', value: 'C'},
] as const;

const myValues = myArray.map((v) => v.value);

when I map through the array returning only the value property, I hoped for the variable myValues type to be:

['A', 'B', 'C']

but instead I got:

('A' | 'B' | 'C')[]

Is it possible to achieve what I'm looking for?

Thanks in advance!

  • No, it's not possible to do this without something like a type assertion. TypeScript's type system isn't expressive enough to say what you're doing; you could hardcode what happens when mapping over a generic callback that does a simple indexing, like [this](https://tsplay.dev/mbBbBm), but it's obnoxious and doesn't scale well. See the answer to the linked question for more information about why this doesn't work. – jcalz Jul 12 '22 at 01:50

0 Answers0