Allow parseAsXLiteral
parsers to return their inputs
#658
Unanswered
neefrehman
asked this question in
Ideas
Replies: 1 comment 1 reply
-
That's the point of using string literals: you are using strings, but they are type-safe by only allowing the right options. One alternative is using an Enum, where you could do: enum Tabs = {
one = 'one',
two = 'two',
three = 'three'
}
const tabsParser = parseAsStringEnum<Tabs>(Object.values(Tabs));
const searchParams = { tabs: tabsParser };
const serializer = createSerializer(searchParams);
const Tabs = () => {
const [currentTab] = useQueryState('tab', searchParams.tab);
return (
<Tabs selectedTab={currentTab}>
<TabList>
<Tab id={tabsParser.one} href={serializer({ tab: Tabs.one })}>One<Tab>
<Tab id={tabsParser.two} href={serializer({ tab: Tabs.two })}>Two<Tab>
<Tab id={tabsParser.three} href={serializer({ tab: Tabs.three })}>Three<Tab>
</TabList>
<TabPanel id={Tabs.one}></TabPanel>
<TabPanel id={Tabs.two}></TabPanel>
<TabPanel id={Tabs.three}></TabPanel>
</Tabs>
)
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm currently implementing a tabs-based interaction with
parseAsStringLiteral
, with a simple example follows:This is quite a lot of string-based lookups, which I think could be made a little safer if we access to a typed object containing the values. (i.e.
Record<T, T>
), at which point we could do:Obviously, this is a bit of a contrived example, and one that can be solved in userland with a function that returns
Record<T, T>
(which I'm doing right now!) But, once we start sharing this parser with other component that may be interested in it (for example in nested components withins aTabPanel
), it becomes more useful for a single parser definition to be able to "bring along" it's options in a type-safe way. This could even include the list of options (T[]
), if we do decide to iterate elsewhere.Curious to hear if you think this would be a sensible addition to the library, or if there's another solution that could work.
Beta Was this translation helpful? Give feedback.
All reactions