Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
-
graphql-code-generator
A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
I don't think "validator" is the right word though - validator implies a thing that tells you whether the data is valid or not (boolean). These "decoders" generally take it a step further, they provide the narrowed type (if the decode is successful). io-ts calls the data structure a "codec", because it can be used to both encode and decode data.
I hadn't heard of Zod! Very similar indeed, I will have to check it out. If you're wondering exactly how it works, check out this earlier piece I wrote.
Neat. I think Zod is the best known solution to this problem but it doesn't work in pure Typescript. This looks like it might which would be great but I also don't see how it could work...
If you don't want any runtime overhead, you should just use a TS types generator or some compile time magic to call your api route functions directly. For GraphQL, there is the amazing graphql-code-generator which generates fully typed functions you can call on the frontend from your schema.
Lol, look what I just found, someone created a whole petition about adding runtime type information (which would make a lot of use libraries redundant): https://www.typescriptneedstypes.com/
Yeah I see what you mean. Indeed all the typing information evaporates after the compilation but I really don't get why it has to be this way to the very end. If you read the ticket https://github.com/microsoft/TypeScript/issues/3628 nowhere the TS team gave any compelling explanation, and it is crazy how many approaches people came up with on how to address it.
Related posts
- Since RN is completely JavaScript based should I create any data classes to define response as a good practice or can I directly access the result of '*fetch()'* for processing the response?
- TypeScript Essentials: Distinguishing Types with Branding
- Headless CMS: Directus vs Payload vs Strapi in 2024
- Type-Safe Fetch with Next.js, Strapi, and OpenAPI
- Integrating GraphQL Codegen with Storyblok: Step-by-Step Guide