Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider mentioning fluent API tree-shakability as a technical motivation #309

Open
TzviPM opened this issue Dec 1, 2024 · 3 comments
Open

Comments

@TzviPM
Copy link

TzviPM commented Dec 1, 2024

In “ Implementation of a modular schema
library in TypeScript with focus on bundle
size and performance” (2003; see https://valibot.dev/thesis.pdf), Fabian Hiller presented valibot as an alternative to preexisting validation libraries that could achieve smaller bundle size by optimizing for tree shaking.

Many JavaScript apis such as RxJS, Valibot, Zod, JQuery, deal with the concept of pipelining. Existing approaches are to either:

  • expose a fluent API (ex: z.string().email().optional() ) which necessarily pulls in large bits of code (i.e. email validation) for smaller tasks (I.e. simple type assertion of a string).
  • use a pipe operator (ex. v.pipe(v.string(), v.email(), v.optional()) ). A Quick Look at npm or GitHub stats shows the popularity of a fluent API over a pipe function.

If support for pipelining were built into the language in a way that its syntax became preferred or even increased popularity of piped apis, it would benefit the ecosystem insofar as tree shakable code leads to smaller bundle sizes and allows libraries to provide more features without requiring consumers to ship unused code.

@adrian-gierakowski
Copy link

💯

@Jopie64
Copy link

Jopie64 commented Dec 1, 2024

fluent API:

z.string().email().optional('arg')

pipe function or method:

z.pipe(v.string(), v.email(), v.optional('arg'))

current hack pipe proposal:

z |> v.string(%) |> v.email(%) |> v.optional(%, 'arg')

Fluent API still looks the most clean. But considering its downside I'd still prefer pipe function over the current hack pipe syntax proposal...

@adrian-gierakowski
Copy link

Downside of pipe function is that it will always have a limit on the number of args if you want to have good typescript support

Being able to use point free style (couple do what curried, data last functions) in a hack pipe would be ideal

z |> v.string |> v.email |> v.optional('arg')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants