Home > Mobile >  How can i set explicit array type without coping and pasting its values in TypeScript?
How can i set explicit array type without coping and pasting its values in TypeScript?

Time:12-30

I have an array:

const mediaFileKeys = ["mp3", "wave", "stems", "image"];

And i need it to have explicit value types.

So i copied and pasted its values:

const mediaFileKeys: ["mp3", "wave", "stems", "image"] = ["mp3", "wave", "stems", "image"];

Is there any way of doing that without having to copy the array values?

CodePudding user response:

Just use the as const assertion:

const mediaFileKeys = ["mp3", "wave", "stems", "image"] as const;
  • Related