What are parsers?
Parsers are the foundation of type safety in nuqs. They define how to convert between URL query string values (always strings) and your application’s typed state values (numbers, booleans, dates, objects, etc.). Every parser implements two core functions:packages/nuqs/src/parsers.ts:7-32
Why parsers matter
Without parsers, all URL state would be strings:Built-in parsers
nuqs provides parsers for common data types:Primitive types
Date and time
Enums and literals
Arrays
packages/nuqs/src/parsers.ts:466-510
JSON objects
Creating custom parsers
UsecreateParser to build parsers with full type safety and builder pattern support:
Parser rules
Always return
null for invalid inputs. Never throw errors in the parse function.Composing parsers
You can compose existing parsers to build more complex ones:Parser builder pattern
All parsers created withcreateParser support a builder pattern for configuration:
Setting defaults
packages/nuqs/src/parsers.ts:79-103
Setting options
Chaining builders
Type inference
UseinferParserType to extract the TypeScript type from a parser:
packages/nuqs/src/parsers.ts:583-588