Bad TypeScript Habits to Break This Year

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • io-ts

    Runtime type system for IO decoding/encoding

  • Type guard in the example should use runtime type checkers or codec like io-ts (https://github.com/gcanti/io-ts), otherwise it will just become a type-assertion with partial correctness.

    With codec, one can make sure that the type is correct and defining type and type-guard becomes a one-time task.

  • type-fest

    A collection of essential TypeScript types

  • Btw in this context I would like to share Type Fast by sindresorhus [1].

    I find it excellent and definitely a helpful tool to think about when you are struggling with type safety vs runtime data.

    1: https://github.com/sindresorhus/type-fest

  • 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.

    SurveyJS logo
  • TypeScript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • According to https://github.com/microsoft/TypeScript/wiki/Performance#pre...

  • runtypes

    Runtime validation for static types

  • Runtypes is another excellent alternative. Highly recommend.

    https://github.com/pelotom/runtypes

  • tslint

    Discontinued :vertical_traffic_light: An extensible linter for the TypeScript language

  • Actually, tslint is deprecated now, and the community effort has moved to typescript-eslint.

    https://github.com/palantir/tslint/issues/4534

  • laminar

    Discontinued OpenApi backed REST APIs. Automatic validation of request / response based on the api schema. (by ovotech)

  • If the api has some contract with it OpenApi / Swagger / etc, its surprisingly easy to write a parser that would convert those to typescript types. TS has an awesome use as a library itself where you can write the ast with, and then tell it to convert it to code.

    We use it to great effect ourselves, by generating types for axios.

    https://github.com/ovotech/laminar/tree/main/packages/lamina...

    Now granted, you’re now trusting the api writers with their contract, but if its another team in the org we’ve found it to be warranted.

  • zod

    TypeScript-first schema validation with static type inference

  • I've also been using Zod for the same purpose - they've got a nice comparison section that included io-ts

    https://github.com/colinhacks/zod#comparison

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • fp-ts

    Functional programming in TypeScript

  • I'm surprised the list doesn't mention untyped-exception and how to deal with the inability to figure out the type from the catch block.

    Untyped exception in TypeScript is one of the most vulnerable point of static type escapes. Unlike any, which is explicit, exception caught in catch block is implicit.

    Personally I prefer to wrap catches into a specific type/symbol `UnknownError { data: unknown }`, and discriminated union to indicate success/failure on return. fp-ts (https://github.com/gcanti/fp-ts) is my go-to library for this

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