#Is there a way to derive a type from a string value?

10 messages · Page 1 of 1 (latest)

shut quiver
#

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");
past fractalBOT
#
alvarny#0

Preview:ts // 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; ...

civic ginkgo
#

I can't think of a way to do exactly what you asked for, but what about something like this?

past fractalBOT
#
smichel17#0

Preview:```ts
...
interface SearchTabContentProps {
activeTab: Tab
}

type Tab =
| {
tab: "spotify"
subTab: SpotifySubTab
}
| {
tab: "trending"
subTab: TrendingSubTab
}
| {
tab: "history"
subTab: HistorySubTab
}

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>("queue")

// So that t
...```

shut quiver
#

Hey thanks for the input! I was also thinking about discriminated unions but I figured there may be a better way, but maybe there also just isn't

past fractalBOT
#

@manic bay Here's a shortened URL of your playground link! You can remove the full link from your message.

kingoberon.#0

Preview:```ts
declare function useState<T>(
initialState: T | (() => T)
): [T, (newState: T) => void]

// 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
...```

manic bay
#

can you explain why? @civic ginkgo Thanks

shut quiver
#

Well because SpotifySubTab is a Type and you're trying to use it as a value here. The possible values for a SpotifySubTab would be "queue", "recently_played" or "top_tracks"

shut quiver