Sinc02
KPCKevin Powell - Community
•Created by Sinc02 on 1/15/2024 in #front-end
Union of literal types from an array of strings
Hi I'm looking for a way to generate a union type for every string in an array.
Let's say I have an array
const sample = ["apple", "banana", "grape", "orange""]
. Basically I want to be able to get type SomeType = "apple" | "banana" | "grape" | "orange"
. I need this SomeType
because I need to create a function that accepts one of these values instead of typing them one by one like this
As you can see it'll be a lot of work to do the typing if the number of items in the array keeps increasing (let's say like 100 items). I just want a union type that consists of the literal types of whatever strings that are in the array (in this case sample
). How can I do this in typescript?6 replies
KPCKevin Powell - Community
•Created by Sinc02 on 12/14/2022 in #front-end
Grid col span to the end if a single item exists in a row
3 replies