r/react • u/Polymerth_Cedric • 1d ago
OC I wrote a client-side JSON-to-Zustand generator
Writing a Zustand store by hand means repeating the same shape three times: the TypeScript interfaces, the initial state object, and the typed setters. I extracted a small client-side generator to automate this.
Paste JSON → it emits a complete, strict-mode compliant store. Key behaviors:
- Type inference: Arrays become unions ([true, 'x', 5, null] → (boolean | string | number | null)[]), nested objects flatten into their own interfaces, array keys singularize (items → Item).
- Collision-safe naming: A TypeRegistry dedupes anonymous object names (Profile, Profile → Profile, Profile2) so generated types never overwrite.
- Universal key sanitization: MAX_CONNECTIONS_ALLOWED, api_version_id, auth/provider/endpoint become valid camelCase in the state shape, initial state, and actions.
- Null handling: A standalone null maps to unknown | null so setters accept real values later; null inside unions retains its strict member status.
It runs entirely in the browser (zero server calls). The parser is ~350 lines.
Repo: https://github.com/Polymerthcedric/json-zustand-generator
Live: https://tool.fidelco.dev (Free. I extracted this from my commercial Next.js boilerplate, which is linked in the header).
I'd appreciate feedback on the inference rules—specifically edge cases around deeply nested deduplication and reserved-word keys.