ts-pattern
🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference. (by gvergnaud)
ts-option
By shogogg
Our great sponsors
ts-pattern | ts-option | |
---|---|---|
38 | 1 | |
10,643 | 91 | |
- | - | |
8.5 | 0.0 | |
6 days ago | about 1 year ago | |
TypeScript | TypeScript | |
MIT License | MIT License |
The number of mentions indicates the total number of mentions that we've tracked plus the number of user suggested alternatives.
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.
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.
ts-pattern
Posts with mentions or reviews of ts-pattern.
We have used some of these posts to build our list of alternatives
and similar projects. The last one was on 2024-02-08.
-
You Don't Need React
ts-pattern has been a decent band-aid for the lack of native pattern matching, but obviously has downsides that could be avoided if it was built into the language.
-
Building React Components Using Unions in TypeScript
ts-pattern
-
TypeScript Is Surprisingly OK for Compilers
With how powerful the type system is you can implement pattern matching via a library pretty convincingly, https://github.com/gvergnaud/ts-pattern is definitely the go-to. That being said pattern matching is hardly a requirement for being ok for implementing compilers.
-
[AskJS] C# in every Node.js job posting?
There's a proposal to add something like that to JavaScript but it's been stuck in limbo since 2017 although there are libraries like ts-pattern which implement it already.
- Rust Appreciation Thread
-
Safer Type Checking At Both Runtime and Compile Time
Otherwise, go for a pattern matching implementation like ts-pattern (which has exhaustiveness checking and has a builder pattern API) or ex-patterns (which im not sure if it has exhaustiveness checking, but it's a little more capable and is inspired by pattern matching in elixir, and has an argument order based API)
-
Safer exhaustive switch statements in TypeScript
Here is a library providing the described logic with a nice interface. I've used it before and others seems to find it helpful too.
-
libraries you are happy that you discovered them
I've use used ts-pattern before to simplify some complex if/elses
ts-pattern: https://github.com/gvergnaud/ts-pattern (pattern matching)
-
Write fewer tests by creating better TypeScript types
Switch statements are extremely limited and suffer from a fall-through hazard that can lead to errors and bad practices. Thankfully, several npm packages can help with type-narrowing discriminated unions, and the ts-pattern library is an excellent choice.
ts-option
Posts with mentions or reviews of ts-option.
We have used some of these posts to build our list of alternatives
and similar projects. The last one was on 2021-10-24.
-
Stop catching errors in TypeScript; Use the Either type to make your code predictable
I've went through an fp-ts phase, used it a lot on a couple side projects, and all it did was create a massive overhead + verbose code for the simplest problems. Way easier & productive to grab a simple option type like ts-option for nullables, sprinkle in some ts-pattern for pattern matching, and then use typescript's built-in discriminated union for variants & result types
What are some alternatives?
When comparing ts-pattern and ts-option you can also consider the following projects:
pattern-grab - 🤛🏻 Regular Expression Data Grabber
ts-adt - Generate Algebraic Data Types and pattern matchers
await-to-js - Async await wrapper for easy error handling without try-catch
union-types - A Typescript library for creating discriminating union types.
typescript-coverage-report - Node command tool to generate TypeScript coverage report
proposal-pattern-matching - Pattern matching syntax for ECMAScript
Index - ⚡ Pattern Matching in Typescript
io-ts - Runtime type system for IO decoding/encoding
zod - TypeScript-first schema validation with static type inference
react-freeze - Prevent React component subtrees from rendering.
rescript-compiler - The compiler for ReScript.