The Potential of Higher-Kinded Types for Library Semantics

Rate this content
Bookmark

There is a wall that we all hit when developing more sophisticated types: how can I 'abstract out' certain commonalities in my type-level logic? How can I make my types more reusable and composable? How can I make my types more expressive?

This talk will introduce the fundamentals of Higher-Kinded Types, a compelling concept hitherto underutilized, and explore how they can significantly enhance the expressiveness of library semantics, leading to a more intuitive developer experience (DX). We'll demonstrate how HKTs can provide an elevated abstraction level, allowing you to model complex domain problems more naturally and in a type-safe manner.

Join us to understand how leveraging HKTs can elevate your TypeScript coding practice, optimizing the semantics and delivering a more potent and featureful library interface.

This talk has been presented at TypeScript Congress 2023, check out the latest edition of this JavaScript Conference.

FAQ

The main topic is the potential of higher kind of types (HKTs) for library semantics in TypeScript.

Michael Potit is an engineering manager at Volley.

HKT Toolbelt is a tool for higher kind of types, created by Michael Potit.

HKTs improve type inference in TypeScript, reducing the need for explicit type declarations.

A 'kind' is a type associated with a type constructor.

An example of a simple kind is 'type foo of T = T', which is a base type.

HKTs allow the creation of type-level operations that can be applied to each element in an array, such as transforming all elements to uppercase.

Kind ratification is the process of giving a callable interface to a higher kind of type, allowing it to be executed recursively across all arguments.

The speaker is Michael Potit, an engineering manager at Volley.

HKTs can be used to create a chaining interface that computes the type-level result as operations are executed and chained together.

Michael Poteat
Michael Poteat
8 min
21 Sep, 2023

Comments

Sign in or register to post your comment.
Video Summary and Transcription
The Talk discusses the potential of higher kind of types (HKTs) for library semantics, enabling better inference and type-level operations. It explains how HKTs can be created in TypeScript by exploiting function types as a function of object attributes. This allows for the creation of composable functional utilities with intelligent type inference and a chaining interface that computes type-level results. Overall, the Talk highlights the benefits and possibilities of using HKTs in software development.

1. Introduction to Higher Kinda Types

Short description:

I'm excited to talk about the potential of higher kinda types for library semantics today. HKTs make better inference possible. We can use HKTs to represent type level operations and compose types together using functional semantics. Higher kind of types are types associated with a type constructor. We want to write code that applies an operation to a map and then applies it to an array, extracting the logic intrinsic to looping over an array and applying a transformation to each element.

Hey there, everyone. I'm excited to talk about the potential of higher kinda types for library semantics today, as part of the TypeScript Congress 2023 lightning talk. First, a little bit about myself. My name's Michael Potit. I'm an engineering manager at Volley, where we are creating the future of voice-enabled entertainment. I have a passion for type-level programming. I've created HKT Toolbelt, which we'll mention a little bit later. You can read about my programming on my blog at code.lol. And I'm excited to talk about the flexibility of higher kinda types in TypeScript today.

So, library semantics. What do we mean by this? So, we want the type system to infer as much about our code as possible. Often, libraries don't do it. So, in this code example, we're using Lodash. We are doing a chain on some sort of object. We're calculating the keys, and then we're mapping those keys to convert them all to uppercase. What we know the value to be is A, B, and C in uppercase, but Lodash just infers an array of strings. So, this is sometimes very inconvenient, and forces us to make explicit type declarations when we otherwise would not have to. So, HKTs basically make better inference possible. We can use HKTs to represent type level operations, and we can compose types together using functional semantics. We can do this so well that end users never have to write any sort of custom types themselves.

So, what are higher kind of types? Basically, a kind is a type associated with a type constructor. In TypeScript, you would know this as like type foo of T equals T, right? The simplest kind is just star, these are your base types. And the next simplest kind is star to star, which is your normal sort of one-erity generic. So, like, capitalize of S or promise of T, etc. So, TypeScript natively supports these two, but we want more. So, we would like to support, for example, a type that takes in a type, sort of generic itself, and a value which represents an array, and we want to loop over that array applying this type operation to each element. So, the kind associated with this would be this sort of complicated expression, which we need to do clever things to support in the TypeScript system. So, what are we actually trying to do? So, what we want to do is we want to write code like this, so we want to say we want to use this operator, or any sort of application operator, and say, oh, right. We want to apply the operation capitalize to map, so that we get map capitalize, which, you know, converts capitalize into something that can be mapped over an array, and then we want to apply that to an array to get a result. So, we're extracting out the logic intrinsic to looping over an array and applying a transformation to each element.

2. Creating Higher Kind of Types in TypeScript

Short description:

This is surprisingly possible in TypeScript. We exploit the fact that function types can be a function of object attributes. We can create functions that behave like higher kind of types. Using this, we can create composable functional utilities with intelligent type inference. We can also achieve a chaining interface that computes type-level results as operations are chained together. That's my lightning talk. Thank you for listening.

This is surprisingly possible in TypeScript. So, here's the minimum definition needed to create this rich set of higher kind of types. Basically, what's going on here is that we are exploiting the fact that on interfaces, the types of functions can be a function of attributes on the object. So, basically, what we're doing is we are filling in the parameter represented by the x element on line two and then extracting out the return type of f.

Here's an example of the identity sort of higher kind of type. What we say identity extends kind and then we just return type of x for the underlying sort of transformation we are representing. So, always the transformation is on the right-hand side.

This is a slightly more complicated example. We don't need to understand or walk through all of this, but essentially on line 11 here, we are applying append to this bar string, which creates a function that appends the string bar to any string that it's given and then we're passing in foo so we get foo bar. What you'll notice is that all arguments are curried in this new semantics. We can also do map, as I mentioned before. Again, we don't need to understand the entire logic here but notably what we're doing is we are taking this append function that we made earlier, we are sort of converting it to something that can be mapped over an array and then we are running it over an array so we get foo bar and baz bar.

All right, so that was a lot of code listings. The question would be, how do we actually use this in real programming? To do this, generally speaking, we want to use a process that I call kind ratification but basically we are bringing down a higher kind of type and we are giving it a callable interface so on line two we're saying the callable function is going to be a generic function that is an input of the kind associated with this type. We are inferring what that literal constant is going to be and then we are basically recursively executing that across all of the arguments that this higher kind of type is returning. I am not including the full implementation here but it's not much more complicated than this. And essentially what this allows us to do is to create functions which behave like higher kind of types.

So using this, we can create this reified map view including the implementation. So we have a map here very simple sort of curried map logic and then we say as reified map and then we have append which is just as reified append and then we can create this result expression where we are sort of mapping over an array with this append statement so that we get hello exclamation mark world exclamation mark. So we've created these composable functional utilities that are sort of that have intelligent type inference associated with them.

So up until now we've used point free semantics to write our code where whereby we're not explicitly mentioning any arguments to our functions but you know more often folks use fluent API design for example in lodash with their chaining semantics so we can actually that using HKTs not including the full implementation but what we want in the end is and what we can get is a chaining interface that actually computes the type level result as you are executing these operations and chaining these operations together. So what we infer in the end is A and B and C.

That's my lightning talk. Thank you so much for listening.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
TypeScript and React: Secrets of a Happy Marriage
React Advanced 2022React Advanced 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.
Remix Flat Routes – An Evolution in Routing
Remix Conf Europe 2022Remix Conf Europe 2022
16 min
Remix Flat Routes – An Evolution in Routing
Top Content
Remix Flat Routes is a new convention that aims to make it easier to see and organize the routes in your app. It allows for the co-location of support files with routes, decreases refactor and redesign friction, and helps apps migrate to Remix. Flat Folders convention supports co-location and allows importing assets as relative imports. To migrate existing apps to Flat Routes, use the Remix Flat Routes package's migration tool.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
Top Content
Daniel Rowe discusses building a TypeScript-first framework at TypeScript Congress and shares his involvement in various projects. Nuxt is a progressive framework built on Vue.js, aiming to reduce friction and distraction for developers. It leverages TypeScript for inference and aims to be the source of truth for projects. Nuxt provides type safety and extensibility through integration with TypeScript. Migrating to TypeScript offers long-term maintenance benefits and can uncover hidden bugs. Nuxt focuses on improving existing tools and finds inspiration in frameworks like TRPC.
Stop Writing Your Routes
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
Designing APIs is a challenge, and it's important to consider the language used and different versions of the API. API ergonomics focus on ease of use and trade-offs. Routing is a misunderstood aspect of API design, and file-based routing can simplify it. Unplugging View Router provides typed routes and eliminates the need to pass routes when creating the router. Data loading and handling can be improved with data loaders and predictable routes. Handling protected routes and index and ID files are also discussed.
How to Make a Web Game All by Yourself
JS GameDev Summit 2023JS GameDev Summit 2023
27 min
How to Make a Web Game All by Yourself
This talk guides you on how to make a web game by yourself, emphasizing the importance of focusing on tasks that interest you and outsourcing the rest. It suggests choosing a game engine that allows distribution on the web and aligns with your understanding and enjoyment. The talk also highlights the significance of finding fun in the creative process, managing scope, cutting features that don't align with the game's direction, and iterating to the finish line. It concludes by discussing the options for publishing the game on the web and leveraging unique web features.

Workshops on related topic

React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced 2022React Advanced 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Building Your Own Custom Type System
React Summit 2024React Summit 2024
38 min
Building Your Own Custom Type System
Featured Workshop
Kunal Dubey
Kunal Dubey
I'll introduce the audience to a concept where they can have end-to-end type systems that helps ensure typesafety across the teams Such a system not only improves communication between teams but also helps teams collaborate effectively and ship way faster than they used to before. By having a custom type system, teams can also identify the errors and modify the API contracts on their IDE, which contributes to a better Developer Experience. The workshop would primarily leverage TS to showcase the concept and use tools like OpenAPI to generate the typesystem on the client side. 
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.