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

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

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • 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.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

  • 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