I'm looking for a solution to a rather simple problem, but my TypeScript knowledge isn't quite there yet apparently :/ Can any of you wizards help me figure this out so that the type corresponds to the string value?
See comments in the code:
// Define Subtabs per Tab
export const spotifySubTabs = ["recently_played", "queue", "top_tracks"] as const;
export const trendingSubTabs = [] as const;
export const historySubTabs = ["purchases", "searches"] as const;
export const wishlistSubTabs = [] as const;
type SpotifySubTab = (typeof spotifySubTabs)[number];
type TrendingSubTab = (typeof trendingSubTabs)[number];
type HistorySubTab = (typeof historySubTabs)[number];
type WishlistSubTab = (typeof wishlistSubTabs)[number];
type SubTab = SpotifySubTab | TrendingSubTab | HistorySubTab | WishlistSubTab;
interface SearchTabContentProps {
activeTab: Tab;
}
const SearchTabContent = ({ activeTab }: SearchTabContentProps) => {
// Now, instead of <SubTab> I would like the type to be dependent on the activeTab value, i.e. if it's "spotify" the Type should be <SpotifySubTab>
const [activeSubTab, setActiveSubTab] = useState<SubTab>();
// So that this becomes impossible
if (activeTab === "spotify") setActiveSubTab("purchases");