Say I had an interface
that described a library that items that look like this:
interface MyItem {
category: string,
title: string
}
Now I have a config file full of those MyItems:
const myLibrary: MyItem[] = [
{
category: "dogs",
title: "Fuzzy quadrupeds"
},
{
category: "snakes",
title: "Slithery reptiles"
},
...
]
Now, I'd like to create a type that consists of all the category
in MyItem[]
If I do this:
type Category = typeof MyItem[number]["category"]
I get string
.
If I remove the typing from myLibrary
(i.e. const myLibrary = [ {...} ]
) and get what I want:
That means that type Category = typeof MyItem[number]["category"]
gives me the union type I want of dogs | snakes
but of course I lose the typing when creating new items in my config file.