Live data from Hacker News

ArkType: Ergonomic TS validator 100x faster than Zod

arktype.io

61–70 of 72 posts

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#61
post #49

ArkType looks anything but ergonomic to me. Typescript in strings? Double level type encoding? It’s a miracle it can be 100x faster than Zod, but speed was never my issue with zod to begin with.

Yeah, that type constraints in strings thing also threw me off big time. Zod is, if you learn it, fairly composable and roughly feels like TypeScript types in terms of intuition on capabilities. Im not sure if I want to sacrifice all of that for a vague promise of performance improvements that I realistically never encounter.

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#62
post #28

Really conflicted with TS. On one hand it’s so impressive that a type system can do these sort of tricks. On the other hand if we had type introspection at runtime we wouldn’t need any of this.

What good would that do with making sure a complex form has been filled out completely? Without a way to meaningfully attach error messages and so on, that only solves a small subset of the problems libraries like ArkType, Valibot, and Zod solve.

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#63

TS: "We have added types to javascript, everything is now strongly typed, and the compiler will pester you to no end until it's happy with the types." Me: "Awesome, so I get an object from an API, it will be trivial to check at runtime if it's of a given type. Or to have a debug mode that checks each function's inputs to match the declared types. Otherwise the types would be just an empty charade. Right?" TS: "What?"…

That isn’t what TypeScript is for, you’re describing a similar but unrelated problem, namely runtime type validation. There’s a reason why JSON.parse() returns any. However. TS allows to avoid logical errors in the program's source, which is a class of errors historically very important, since JavaScript is so highly dynamic.

The debug mode sounds interesting at first thought, but quickly explodes in complexity when you deal with more complex object types and signatures. To enable automatic runtime validation for all cases, you would need to rewrite programs so thoroughly that you’re pretty much guaranteed to introduce bugs and behaviour changes that were not present in the source code.

In my opinion it’s great that TS draws a strict boundary to avoid runtime impact at all cost, and leave that to libraries like Zod, which handle dealing with external data.

> to catch all the "somebody fetched a wrong type from the backend" or "someone did some stupid ((a as any) as B) once to silence the compiler and now you can't trust any type assertion about your codebase" problems. Nada.

Those type casts are sure annoying, but what’s the alternative? Even in your hypothetical debug mode, you would not be safe here, since you’re effectively telling the compiler you know better and it’s supposed to transform that type, not assert it. Or do you want to remove the escape hatch „as“ is? Because that would be a major pain in the ass in situations where you just do know better, or don’t want to ensure perfect type safety for something you know will work.

You can’t make things idiot proof, no matter how hard you try. That doesn’t make preprocessing type hints useless.

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#64
post #62
post #28

Really conflicted with TS. On one hand it’s so impressive that a type system can do these sort of tricks. On the other hand if we had type introspection at runtime we wouldn’t need any of this.

What good would that do with making sure a complex form has been filled out completely? Without a way to meaningfully attach error messages and so on, that only solves a small subset of the problems libraries like ArkType, Valibot, and Zod solve.

Look at how it’s done in Python, with Pydantic for example. The runtime information allows you to write plain typed functions (for example in FastAPI), and you can be 100% confident these are the types your function will actually receive at runtime, without having to write the types in a separate DSL. This is not possible in standard TS.

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#65
post #37

In validation, it's never about speed. Is how you relate the schema tree to the error reporting tree. If you didn't already, you will figure that out eventually. If you mess that (either by being too flat, too customizeable or too limited), library users will start coming up with their own wrappers around it, which will make your stuff slower and your role as a maintainer hell. (source: 15 years intermittently mainta…

I find your last paragraph to be lacking. Would help if you elaborated more on the purported need for this "bridge" concept.

Maybe one day I will elaborate on it!

The need is obvious. As natural language becomes more proeminent in programming, there will be a need for a bridge to the older traditional paradigms. I can't give more details, it's the kind of thing you can't put in prose yet.

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#66
post #50
post #37

In validation, it's never about speed. Is how you relate the schema tree to the error reporting tree. If you didn't already, you will figure that out eventually. If you mess that (either by being too flat, too customizeable or too limited), library users will start coming up with their own wrappers around it, which will make your stuff slower and your role as a maintainer hell. (source: 15 years intermittently mainta…

Especially when unions are involved. We have a flexible schema for many parameters (e.g., X accepts an object or an array of them, and one object has some properties that could be an enum or a detailed shape), and both Zod and Valibot produce incomprehensible and useless error messages that don’t explain what’s wrong. We had to roll our own.

Have you tried applying boolean algebra conversions on the schema? Some ORs can be written as NOT AND expressions. I don't know about these JS versions, but in my lib the choice of boolean expression had influence on th3 error output.

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#67
post #63

TS: "We have added types to javascript, everything is now strongly typed, and the compiler will pester you to no end until it's happy with the types." Me: "Awesome, so I get an object from an API, it will be trivial to check at runtime if it's of a given type. Or to have a debug mode that checks each function's inputs to match the declared types. Otherwise the types would be just an empty charade. Right?" TS: "What?"…

That isn’t what TypeScript is for, you’re describing a similar but unrelated problem, namely runtime type validation. There’s a reason why JSON.parse() returns any. However. TS allows to avoid logical errors in the program's source, which is a class of errors historically very important, since JavaScript is so highly dynamic. The debug mode sounds interesting at first thought, but quickly explodes in complexity when…

The alternative would be to use a different programming language altogether. In programming languages that have built-in static types, parsing JSON works differently. It’s not possible to lie to the compiler so that a variable declared to be a string actually contains something else. You have to use a parser that will construct the specific type you want.

The closest thing to JavaScript would probably be Dart, now that it has sound types [1].

You’re right that it’s a pain in some situations.

[1] https://dart.dev/language/type-system

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#68

With many TS features making their way into JS, I've sometimes wondered if TS is to JS what Sass is to CSS. I currently rely on TS, but I now consider Saas harmful [there being overlapping syntax w/ vanilla CSS].

What features are you thinking of? I hadn’t heard anything about TS types making their way into JS in any way (unless you count engines that can execute TS directly, but those just ignore the type syntax). That would be a massive change.

https://www.totaltypescript.com/erasable-syntax-only

https://tc39.es/proposal-type-annotations/

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#69

Earlier quoted context omitted.

What features are you thinking of? I hadn’t heard anything about TS types making their way into JS in any way (unless you count engines that can execute TS directly, but those just ignore the type syntax). That would be a massive change.

https://www.totaltypescript.com/erasable-syntax-only https://tc39.es/proposal-type-annotations/

Hmm, I don’t think either of those warrants any concern about “TS features making their way into JS” or “overlapping syntax”.

The first one is removing a TS feature that failed to make its way into JS, and the second is about explicitly carving out a space in the syntax so that you can use TS (or Flow!) in JS codebases without being locked into any particular tooling.

Re: ArkType: Ergonomic TS validator 100x faster than Zod

#70
post #64
post #62

Earlier quoted context omitted.

What good would that do with making sure a complex form has been filled out completely? Without a way to meaningfully attach error messages and so on, that only solves a small subset of the problems libraries like ArkType, Valibot, and Zod solve.

Look at how it’s done in Python, with Pydantic for example. The runtime information allows you to write plain typed functions (for example in FastAPI), and you can be 100% confident these are the types your function will actually receive at runtime, without having to write the types in a separate DSL. This is not possible in standard TS.

Well look at how the Python ecosystem squirmed real Pythonesque when adopting these type hints; and that's a packaged runtime that can just bump the major version and do a hard breaking change.

That doesn't work for JavaScript; you need full backwards compatibility at all times. Porting runtime type information to JS would change the language innards so much, it'd just be a different language in the end. At that point we could equally argue whether browser vendors should ship a Python runtime in browsers and add support for .

Post reply on HN