Our great sponsors
-
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.
-
laminar
Discontinued OpenApi backed REST APIs. Automatic validation of request / response based on the api schema. (by ovotech)
-
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.
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.
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
According to https://github.com/microsoft/TypeScript/wiki/Performance#pre...
Runtypes is another excellent alternative. Highly recommend.
https://github.com/pelotom/runtypes
Actually, tslint is deprecated now, and the community effort has moved to typescript-eslint.
https://github.com/palantir/tslint/issues/4534
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.
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
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