gitmyhub

hotscript

TypeScript ★ 3.7k updated 1y ago

A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.

A TypeScript library that lets you transform and manipulate types using readable functional patterns like Pipe, map, and filter instead of deeply nested, hard-to-follow type expressions.

TypeScriptsetup: easycomplexity 3/5

HOTScript is a TypeScript library that lets you write complex transformations at the type level using the same functional programming patterns you would use when writing regular runtime code. In TypeScript, types describe the shape of data (what fields an object has, what kind of values a function accepts), but manipulating those types in sophisticated ways normally requires verbose, hard-to-read syntax. HOTScript introduces a set of composable, named functions for types so the logic reads more like normal code.

The core idea is a Pipe operator that passes a type through a chain of transformations, one step at a time. For example, you can start with a list of numbers, add a value to each one, join them into a string, split them again, and sum the results, all expressed as a sequence of named steps rather than deeply nested type expressions. This makes the intent of the type logic much easier to follow.

The library includes functions for working with tuples (typed arrays), objects, strings, numbers, and booleans. There are functions for mapping, filtering, flattening, grouping, sorting, and doing arithmetic. You can also define your own reusable type-level functions, which the library calls lambdas, and pass them as arguments to higher-order functions like map and filter, the same way you would in JavaScript.

A practical use case shown in the README is parsing a URL route pattern like /users/id:string/posts/index:number directly into a TypeScript object type with the correct field names and types. Another example shows making the browser's querySelector function fully type-safe based on the CSS selector string you pass in.

This is a developer tool for TypeScript authors who want to write advanced type utilities with less boilerplate. It is published on npm and is a work in progress, so the API may change between releases.

Where it fits