Ensuring Typescript Code Quality With expect-type

Rate this content
Bookmark

TypeScript's static typing catches many errors, but how do we ensure our type definitions themselves are correct? This talk introduces type testing and demonstrates its implementation using expect-type, a lightweight library for exactly this.

We'll cover:

- The concept of type testing and its importance in TypeScript projects
- A deep dive into expect-type's features and API
- Practical examples of using expect-type to catch subtle type errors
- Integrating expect-type into existing projects and CI pipelines
- Advanced use cases and best practices

Drawing from real-world scenarios, we'll explore how expect-type can be used as a standalone library, or through Vitest which bundles it natively, to enhance type safety and code quality.

Suitable for TypeScript developers of all levels, this talk will equip you with the tools and knowledge to write more robust, type-safe code using expect-type.

This talk has been presented at JSNation US 2024, check out the latest edition of this JavaScript Conference.

FAQ

Expect Type can help improve your TypeScript skills by allowing you to evaluate how TypeScript interacts with your code base and by documenting the type behavior within your team.

Expect Type error messages are integrated with the TypeScript CLI, providing detailed messages that help identify mismatches between expected and actual types.

No, Expect Type does not require any additional build steps or IDE extensions, and it has no dependencies, ensuring no bloat is added to your node modules.

Yes, Expect Type can be used in production code without any performance concerns since it introduces no runtime overhead.

Expect Type supports testing for various TypeScript features, including complex types, type guard functions, and generic type testing.

More information about Expect Type is available on its NPM page, GitHub repository, and VTest documentation. The author is also available on X under mmcalc.

Expect Type is a library for type-level testing in TypeScript, allowing developers to write tests for their types similar to how they write tests for runtime behavior.

Expect Type can be used with any test framework that supports TypeScript, including Jest, Mocha, and Playwright, with built-in support available for VTest.

When using Expect Type, focus on testing edge cases and be selective about what you test. It's more useful for libraries than applications and should be run as part of your CI pipelines.

Misha Kaletsky
Misha Kaletsky
9 min
21 Nov, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Expect Type is a library for type-level testing in TypeScript. It allows you to write tests for your types, ensuring that they stay what you want them to be as your code base evolves. The library supports a wide range of TypeScript features, including type guards, advanced features, and generic type testing. It also integrates with various test frameworks, including VTest. Best practices include testing only helpful things, making generics specific, and running tests as part of CI pipelines. Understanding error messages and fixing type errors are important aspects of using the library.

1. Introduction to Expect Type library

Short description:

Expect Type is a library for type-level testing in TypeScript. It allows you to write tests for your types, ensuring that they stay what you want them to be as your code base evolves. This library is a good way to improve your TypeScript skills and understand how TypeScript interacts with your code base. It has no build step, no dependencies, and no runtime overhead. You can use it in production code for complex types without performance concerns. Basic usage involves making assertions on values using the expect type function.

Hi, my name's Misha Koletsky. I'm a software engineer and I'm the author of Expect Type, a library for type-level testing in TypeScript. So, what is it? It is a library available through npm that allows you to write tests for your types. So, if you're familiar with writing tests for runtime behavior, this is much the same thing, and it will be very familiar syntax to anyone who's familiar with the Expect functionality of Jest or VTest, but this lets you write tests for the actual types of your TypeScript code instead of the runtime behavior or the actual values.

This is useful for making sure that your types are what you think they are, but it also makes sure that they stay what you want them to be as time goes on. So, it can help protect you against colleagues or your future selves from making a mistake to regress types, to add anys and that sort of thing as your code base evolves. This also is a really good way to improve your own TypeScript skills. So, if you want to learn more about TypeScript and evaluate how TypeScript interacts with your code base, this is a really good way of doing it and kind of documenting how TypeScript works on your team.

All of this comes with no build step. So, there's no extra CLI that you need to run. There's no IDE VS code extension that you need to install or anything like that. Everything will just show up in whatever IDE you use, and the errors, if there are any, will show up when you run TSC, the TypeScript CLI, which hopefully you're already doing if you're using TypeScript. There are no dependencies, so there's no bloat that it adds to your node modules. If you look at the GitHub after this talk, you'll see it's just a handful of TypeScript files. There are some gnarly generics in its implementation, but none of that should be anything that you need to worry about as a user of expect type. There's no multi-megabyte patch version of TypeScript that ships with it. It's just pretty simple types. And there's also no runtime overhead. So, we have seen some teams using expect type in their production code, in fact. It doesn't have to be run as part of a test suite.

So, if you have very, very complex, generic, inferred types that it's very hard to access from other modules, including test code, you can actually just put expect type assertions right in your production code, and there's no performance concern because all of the functions that come with the library are no upset runtime. So, you don't need any special compiler tricks in order to avoid performance overhead. And like mentioned, there are no dependencies either.

This is what basic usage looks like. The main export is this expect type of function that you will pass a value to, and then you'll basically make assertions on that value. So here, we're saying that we expect 42 to be a number. We expect hello to be a string, which is obviously something that you would expect. In real life, you wouldn't test literal values, but you would import something, say, from your production code, and make sure that a certain value is a number or is a string. It can also do complex types. So, here's a barely more complex example of a coffee order.

2. Advanced Features and Generic Type Testing

Short description:

We can write assertions to ensure that objects have specific properties and types. Expect type supports a wide range of TypeScript features, including type guards, advanced features, and generic type testing. It also allows you to test for properties that should or should not be present in an object.

We probably want to go further than just saying the coffee order is an object. We want to make sure that it's an object of a certain type. So here, we write an assertion that says that the coffee order has two properties, one which is called type, which is a string, and one which is called quantity, which is a type number.

There are lots more features. So, pretty much every feature you can think of in TypeScript, anything that has native functionality, you can test with expect type. So, a pretty commonly used one that there's an example of here is that we have two values, both of which have legs, but have other properties that maybe aren't relevant to this test. So, you can write, we've written two assertions here of the same form. One, we've made sure that horse has type of legs number, and so does a table. But obviously, we don't need to worry about those extra properties like a table usually doesn't have a name and a horse usually doesn't have a material. So, this is similar to just to match object helper, if you're familiar with that. More advanced features of TypeScript are supported as well.

So, here we've got a type guard function where we're making sure that the input to a function is of type date. So, you can check that the function returns a Boolean, but you can also check that it guards inputs of type date. There are lots of examples like this. There's a similar asserts helper. It's really worth exploring the documentation to find them all. And generic type testing as well. So, you don't have to pass a value into the expect type of function, you can just pass a generic type that doesn't actually exist at runtime at all. So, here we've got a magic box which has contents and its locked property. And here we're passing it as a generic type into the expect type of function and we're making sure that the contents property has type number.

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.
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
Top Content
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.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
This talk discusses the performance issues in TypeScript builds and introduces a new feature called isolated declarations. By running the compiler in parallel and using isolated modules, significant performance gains can be achieved. Isolated declarations improve build speed, compatibility with other tools, and require developers to write types in code. This feature has the potential to further increase performance and may be available in TypeScript soon.
Full-stack & typesafe React (+Native) apps with tRPC.io
React Advanced 2021React Advanced 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Top Content
Alex introduces tRPC, a toolkit for making end-to-end type-safe APIs easily, with auto-completion of API endpoints and inferred data from backend to frontend. tRPC works the same way in React Native and can be adopted incrementally. The example showcases backend communication with a database using queries and validators, with types inferred to the frontend and data retrieval done using Prisma ORM.

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. 
Frictionless Development With Unified Type System
JSNation 2024JSNation 2024
113 min
Frictionless Development With Unified Type System
Featured Workshop
Ejiro Asiuwhu
Ejiro Asiuwhu
Imagine developing where frontend and backend sing in harmony, types dance in perfect sync, and errors become a distant memory. That's the magic of TypeScript Nirvana!
Join me on a journey to unveil the secrets of unified type definitions, the key to unlocking frictionless development. We'll dive into:
- Shared language, shared love: Define types once, share them everywhere. Consistency becomes your BFF, errors your worst nightmare (one you'll rarely see).- Effortless coding: Ditch the manual grind of type checking. TypeScript's got your back, freeing you to focus on building awesomeness.- Maintainability magic: With crystal-clear types guiding your code, maintaining it becomes a walk in the park. More time innovating, less time debugging.- Security fortress: TypeScript's type system shields your app from common vulnerabilities, making it a fortress against security threats.