-
I have a code like the following and get a type error export type OrderByDirection = 'desc' | 'asc';
const [direction, setDirection] = useQueryState<OrderByDirection>(
'direction',
parseAsString.withDefault('desc'), // get a type error here
); if I remove the I have found a walk around like this. However, could the code work like the previous one? const [direction, setDirection] = useQueryState<OrderByDirection>(
'direction',
(parseAsString as ParserBuilder<OrderByDirection>).withDefault('desc'),
); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You'll need the parser to be a bit smarter about the runtime value if you want to achieve type-safety. The string parser doesn't check values (it doesn't do anything actually, it's a pass-through). What you want is the const orderByDirection = ['asc', 'desc'] as const;
export type OrderByDirection = (typeof orderByDirection)[number];
const [direction, setDirection] = useQueryState(
'direction',
parseAsStringLiteral(orderByDirection).withDefault('desc')
); |
Beta Was this translation helpful? Give feedback.
You'll need the parser to be a bit smarter about the runtime value if you want to achieve type-safety. The string parser doesn't check values (it doesn't do anything actually, it's a pass-through).
What you want is the
parseAsStringLiteral
parser: