I've been working on a little side project and I just wanted to share it with you so that maybe you can find some use for it in your own projects.
typed
is similar to superstruct and zod in that it validates data at runtime and it is type-safe but what's different about it is that it comes in a tiny package (1kb) with an ESM build and it also outperforms them by a significant amount.
Here is where my original post would have ended, but somebody mention in the comments that I should tease you with some examples (yes, my marketing skills suck!).
So before I show some examples let me explain why I made it in the first place. The first reason was curiosity: build it to understand how other libraries work. typed
actually borrows some type definitions from superstruct
because when I started writing this library I had more limited knowledge about more advanced TypeScript topics.
The second reason was that I needed to complement a library I made for writing type-safe JSON APIs. It started as a nicer way to write next.js handlers and I'll soon migrate to the server (yes, I know all cool kids are writing Serverless code this days but I still enjoy coding good old node servers) but I needed a library to validate incoming JSON and in case of failure, reply with a 400 Bad Request error.
The third reason is that I stopped using GraphQL on my personal projects and I switched back to plain fetch requests using Vercel's SWR (stale while revalidate). The thing is that I was missing all the type-safe features that apollo
and codegen
was giving me.
To demonstrate the second reason, let me show you how I'd like to write a restful endpoint:
import * as T from 'typed'
enum PostStatus {
draft,
published,
}
const inputType = T.object({
title: T.string,
body: T.string,
tags: T.array(T.string),
status: T.enums(PostStatus),
publishedAt: T.optional(T.asDate),
})
// The request handler
export const createPost = post(async (request) => {
const data = await request.json(inputType);
const post = await prisma.post.create({ data });
return Response.json(post, HttpStatus.CREATED)
})
To demonstrate the third reason, here's is a practical example that you can explore on codesandbox.
Anyways, I hope you find it useful as I do on my own projects. Feel free to reach out if you have any questions.
Typed
A blazing fast, dependency free, 1kb runtime type-checking library written entirely in typescript, meant to be used with it.
There are dozens of validation libraries out there, so why create yet another one? Well, I tried almost every library out there and there is only one that I really like called superstruct
(which is awesome) that provides almost everything that I want, but still, I wanted to create my own. The others are simply bloated or don't provide proper typescript support. So that's where typed
comes in.
typed
is all about function composition. Each function is "standalone" and provides a safe way to validate data, you don't need a special kind of function to execute a schema against some value. All functions return a special type which is either Success<T>
or Failure
. If success
is true
then value
is available and fully typed and if not, errors
is…
Top comments (7)
Hi there, glad you like it! I took you advice and edited the post to give a little more context and code examples. I suck when it comes to promoting / demonstrating what I've done, I kinda throw it out there and hope people use it. Also, english is not my primary language so it is a little hard for me to find the right words sometimes. Anyways, thanks for the feedback!
Great work, wish you success!
Offtopic, but what's wrong with GraphQL on personal projects? If the problem is the huge bundle size of apollo, I tried going with graphql + codegen + react-query and I enjoyed that experience
There is nothing wrong with GraphQL at all. Sometimes it just doesn't justify the bundle size + setup complexity. I enjoy using both REST and GraphQL but I tend to lean more to minimalistic and performant solutions whenever I can. Also, this library I made is more general purpose in that it can be used in other areas. You can take a look at how I'm using it here: github.com/brielov/resty
This is perfect! Just what I was looking for! Thank you very much!
I browsed a repo you have (nomocms) and damn! I've never experienced the feeling of somebody else using something I wrote. You made my day. Thank you so much <3
That looks awesome!
Thank you! Glad you liked it (: