Unlocking type-safety superpowers in Typescript with nominal and refinement types

This page summarizes the projects mentioned and recommended in the original post on reddit.com/r/typescript

Our great sponsors
  • Appwrite - The Open Source Firebase alternative introduces iOS support
  • SonarLint - Clean code begins in your IDE with SonarLint
  • Scout APM - Truly a developer’s best friend
  • InfluxDB - Build time-series-based applications quickly and at scale.
  • talent.io - Download talent.io’s Tech Salary Report
  • nominal

    🔒 The new way to do types in typescript.

    Hey OP here, Wanted to share something we recently discovered in typescript. This is a way to to create haskell style Newtype using the lesser known \`symbols\` ```ts type Minutes = number type Seconds = number const minutesToSeconds = (minutes: Minutes) => minutes * 60 const seconds: Seconds = 420 // uh-oh, we can use Minutes and Seconds interchangeably minutesToSeconds(seconds) ``` Nominal types solve this problem ```ts import { Nominal, nominal } from 'nominal-types'; type Minutes = Nominal<'Minutes', number>; type Seconds = Nominal<'Seconds', number>; const minutesToSeconds = (minutes: Minutes) => minutes * 60 // You can directly type cast or use nominal.make const seconds = nominal.make(420) const minutes = 1337 as Minutes // doesn't work, yay type safety minutesToSeconds(seconds) // does work! minutesToSeconds(minutes) ``` We have some lot cooler examples https://github.com/modfy/nominal#examples You can also read more about it here: https://zackoverflow.dev/writing/nominal-and-refinement-types-typescript

  • zod

    TypeScript-first schema validation with static type inference

    Not a huge fan of it personally but zod (https://github.com/colinhacks/zod) is a way to do some runtime type checking.

  • Appwrite

    Appwrite - The Open Source Firebase alternative introduces iOS support . Appwrite is an open source backend server that helps you build native iOS applications much faster with realtime APIs for authentication, databases, files storage, cloud functions and much more!

  • io-ts

    Runtime type system for IO decoding/encoding

    TS doesn't do anything at runtime no, but there are some type checking libraries that could help you. I don't know what packages you're talking about in particular, but if they are IO related you can try io-ts. More generally you could also look into true-myth for Maybe types and wrap any values that come out of your packages before allowing them to be passed into your code.

  • true-myth

    A library for safer and smarter error- and "nothing"-handling in TypeScript.

    TS doesn't do anything at runtime no, but there are some type checking libraries that could help you. I don't know what packages you're talking about in particular, but if they are IO related you can try io-ts. More generally you could also look into true-myth for Maybe types and wrap any values that come out of your packages before allowing them to be passed into your code.

  • typescript-runtime-type-benchmarks

    📊 Benchmark Comparison of Packages with Runtime Validation and TypeScript Support

    I've been using it since then, because it's good enough for my purposes and its popularity makes it more likely to be relevant in my next job search. I have no complaints, but because zod is orders of magnitude slower than its peers, it's not the best option for every context.

  • type-fest

    A collection of essential TypeScript types

    Relatively the same approach as type-fest's opaque type

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts