This list is a collection of typescript libraries that I've found useful. Consider it my public wall of github starts.

TS-Pattern

ts-pattern is a super simple pattern matching library for typescript. It's most useful for doing exhaustive switch/case statements, and narrowing types.

import { match } from "ts-pattern";

type FeedItem =
  | {
      type: "post";
      title: string;
      contents: string;
    }
  | {
      type: "image";
      src: string;
      caption: string;
    };

const Feed = (items: FeedItem[]) => {
  return (
    <div>
      {items.map((item) => (
        <FeedItem item={item} />
      ))}
    </div>
  );
};

const FeedItem = (item: FeedItem) =>
  match(item)
    .returnType<React.ReactElement>()
    .with({ type: "post" }, (post) => (
      <Post title={post.title} contents={post.contents} />
    ))
    .with({ type: "image" }, (image) => (
      <Image src={image.src} caption={image.caption} />
    ))
    .exhaustive();