Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typescript-pattern-matching

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-pattern-matching

Pattern matching for TypeScript

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
37
decreased by-69.42%
Maintainers
1
Weekly downloads
 
Created
Source

Pattern Matching for TypeScript

Paattern matching allows programmers to compare data with defined structures to easily pick one of the available expressions. Many languages that are designed as ‘functional programming languages’ have built-in keywords for pattern matching. Well know examples are F# (match … with) or Haskell (case … of). One language that works very well with functional programming but lacks those features is TypeScript. This library adds support pattern matching to TypeScript.

Read the article about this library for more context.

Features

The features of this library include:

  • Value patterns
  • Record patterns
  • Wildcard patterns
  • Good type inference based on the patterns
  • Negated patterns (including negated type inference)
  • Additional predicates (like when)

Quick Examples

type Option<a> = { kind: 'none' } | { kind: 'some', value: a }

let val: Option<string> = { kind: 'some', value: 'hello' }

match(val)
  .with({ kind: 'some' }, o => o.value)
  .run()
let blogOverviewResponse: any = /* ... */

match<any, Blog[] | Error>(blogOverviewResponse)
  .with([{Id: Number, Title: String}], r => r.map(b => ({id: b.Id, title: b.Title})))
  .with({ errorMessage: String },      r => new Error(r.errorMessage))
  .otherwise(                         () => new Error('client parse error'))
  .run()

About

This library is written by Wim Jongeneel

Keywords

FAQs

Package last updated on 15 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc