typebox
sweet.js
Our great sponsors
typebox | sweet.js | |
---|---|---|
57 | 12 | |
4,227 | 4,580 | |
- | 0.0% | |
8.8 | 0.0 | |
3 days ago | over 6 years ago | |
TypeScript | JavaScript | |
GNU General Public License v3.0 or later | BSD 2-clause "Simplified" License |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
typebox
-
Popular Libraries For Building Type-safe Web Application APIs
The documentation can be found here.
-
I write HTTP services in Go after 13 years (Mat Ryer, 2024)
So far I like the commonly used approach in the Typescript community best:
1. Create your Schema using https://zod.dev or https://github.com/sinclairzx81/typebox
2. Generate your Types from the schema. It's very simple to create partial or composite types, e.g. UpdateModel, InsertModels, Arrays of them, etc.
3. Most modern Frameworks have first class support for validation, like is a great example Fastify (with typebox). Just reuse your schema definition.
That is very easy, obvious and effective.
- Where DRY Applies
-
Has anybody used Typia library?
There's a ton of schema validators out there and most devs have their personal favorite. Mine was zod and is now typebox + ajv.
- I'm looking to use my openapi spec to dyanamically create types
-
How can I generate typescript types?
If you're willing to document your API with an OpenAPI schema, then it should be possible to generate TypeScript types based on the OpenAPI schema with something like openapi-typescript. Also, Typebox can generate JSON schemas, maybe it can be used to generate something that the front-end can also use?
-
[AskTS] What do you think will be the future of runtime type checking?
Well, I do provide extensibility for those bullet points you've listed to varying degrees (both schema and type representation), as well as offering a reference implementation for expressing a entirely different schema specification under the type system (specifically RFC8927 / JSON Type Definition). Reference implementation here. As for JSDoc, It's supported in code hints.
- TypeBox: Runtime Type System Built on Industry Standards
- TypeBox: A Type System for JavaScript built on Industry Standard Specifications
-
Building a modern gRPC-powered microservice using Node.js, Typescript, and Connect
In setting out to build this service, we wanted to use gRPC for its APIs. We’ve been reaching for REST when building APIs so far, primarily out of necessity, i.e., our public APIs needed auto-generated client SDKs and docs for developers working with them. We built those APIs with Fastify and Typebox but felt burned by a code-first approach to generating an OpenAPI spec. I’ll spare you the details and save that experience/learning for another article. Suffice it to say we love gRPC’s schema-first approach. This blog post summarizes our feelings well
sweet.js
-
JavaScript Macros in Bun
I was ready to be excited from the title, but was utterly disappointed :(
IMO these aren’t macros in the Lisp-sense of the word (or Rust, or even C); yeah they run code at compile time, but that’s where the common ends.
Macros should be able to apply syntactic transformation on the code. Lisp is famous for allowing that by representing code as lists. Rust has a compiler-level API to give tokens and run arbitrary code, then spit new tokens out. C macros operate on the tokens level, so with enough magic you can transform code to the shape you want.
This… isn’t any of that.
A pretty good example (and something I’m still sad that it didn’t take off) of macros in JS is Sweet.js[0]. Babel macros[1] are a bit higher level, where macros require the input to already be a valid AST, but that’s also cool.
This… I’d say it’s more of a build-time code execution feature, not a macro feature.
[0]: https://www.sweetjs.org/
Macros I'd expect to be able to be able to expand at compile time to more code to reduce boilerplate, something like SweetJS.
-
Angular Signals: What? Why? and How? (Classy explanation of what we know as writables.)
But yes, I'll tone down my appreciation for writables in this sub, I'll confine my tangents to svelte. A kind of a shame I'd love to talk about using https://www.sweetjs.org/ to make the $ autosubscription work outside of svelte, and the like. But holy off-topic Batman!
- Sweet.js - Hygienic Macros for JavaScript (Macros allow you to sweeten the syntax of JavaScript and craft the language you always wanted)
- Interesting... Macros for JavaScript?
-
Pipe Operator (|>) For JavaScript
Mozilla created SweetJS over a decade ago[0]. It added hygenic macros to JS and I'm sure everyone on the TC39 committee is familiar with it.
There's a lot to like about it, but macros in such a complicated language as JS are hard to get right. They'd also potentially lead to huge fracturing in the JS ecosystem with different factions writing their own, incompatible macro-based languages.
Look at JSX for an example. It's actually a subset of a real standard (E4X -- actually implemented in Firefox for a long time), but just one relatively small syntax addition has added complexity elsewhere.
For example, `const foo = (x:T) => x` is valid Typescript for a generic arrow function, but is an error if your file is using JSX.
I like the idea of macros, but I suspect they made the right call here.
[0] https://www.sweetjs.org/
-
Ultra-minimal JSON schemas with TypeScript inference
Sweetjs is unfortunately dead [0] for like 5 years now.
It also doesn't have any typescript awareness which is required to build this kind of functionality - you want to have static type introspection available in macros so you can generate code based on provided types.
[0] https://github.com/sweet-js/sweet-core/graphs/contributors
-
Equivalent to macros, ifdef and enums from other programming language?
None of these are supported natively by JavaScript. Macros and ifdefs you could get with your own build step using things like sweetjs or ifdef-loader for webpack. Enums are in a similar boat with TypeScript supporting enums out of the box. But there's also a (very early) proposal to add enums natively to JavaScript as well.
-
What are some reasons to use metaprogramming?
TS doesn’t have monadic do notation, which is something that I want very badly. This would be easy to add to JavaScript with Sweet.js macros, but there’s no equivalent tool for TypeScript. I expect that extending this to TypeScript would be very difficult.
-
SICP: JavaScript Edition available for pre-order
> If forEach is not to be used
That source is simply incorrect. forEach is perfectly fine to use as long as you realize what is happening. It iterates an array where each thing is a function that returns a promise. Of course the results aren't what they expect.
ES5 array additions suffer from being a little too early (though later stuff wouldn't exist otherwise, so...). They are designed to deal with holey arrays (arrays with indexes missing). This is extremely uncommon today, but was decently common once upon a time. They were also created before the iterator protocol.
The real fix is to design iterator versions that can handle things like async generator functions.
> it should be deprecated and removed from the language in a timely manner.
NOTHING can be removed from the language once added. Doing that would break all the older websites that depend on it (technically, a few minor breaks happened after they tested millions of sites and couldn't find anything that was adversely affected). At best, they can block older features from newer features. For example, using class syntax or a bunch of other ES6 language structures automatically makes your code shift into "use strict" mode.
I hope they introduce a "use strict 2" variant that strips away more of the undesirable features than the current "use strict" does.
> Since Javascript is based on C syntax, it should have the preprocessor that the birthplace of C saw it fit for that language not to be without.
That pre-processor was a source of untold nightmares. Direct injection leads to bugs. If someone is going that route, full-blown macros are the only answer. There is a full-blown macro system Mozilla created a few years ago, but it's not very popular.
https://www.sweetjs.org/
There also exist some C-style pre-processors for babel too, but they should be avoided because lisp's gensym is a critical feature that they and C both lack.
What are some alternatives?
zod - TypeScript-first schema validation with static type inference
Zepcode - ⚗️ Zeplin extension that generates Swift snippets from colors, fonts, and layers
zod-to-json-schema - Converts Zod schemas to Json schemas
ts-morph - TypeScript Compiler API wrapper for static analysis and programmatic code changes.
ajv - The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
medium-editor - Medium.com WYSIWYG editor clone. Uses contenteditable API to implement a rich text solution.
class-validator - Decorator-based property validation for classes.
typescript-transformer-handbook - 📘 A comprehensive handbook on how to create transformers for TypeScript with code examples
openapi-typescript-validator - Generate typescript with ajv validation based on openapi schemas
ace - Ace (Ajax.org Cloud9 Editor)
ts-json-schema-generator - Generate JSON schema from your Typescript sources
CodeMirror - In-browser code editor (version 5, legacy)