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