Check out a free preview of the full Intermediate TypeScript course:
The "Inference Q&A" Lesson is part of the full, Intermediate TypeScript course featured in this preview video. Here's what you'd learn in this lesson:

Mike answers a student's question regarding if the TypeScript team plans on making the type ConstuctorArg a standard type and discusses the benefits of creating package libraries of useful custom types.

Get Unlimited Access Now

Transcript from the "Inference Q&A" Lesson

>> So Frank asks, this was a few minutes ago. So we made this type ConstructorArg, right, that was kind of what we ended up with, this was the last thing that we worked on in the previous chapter. Does the TypeScript team plan to make this a standard type, it seems pretty useful, same with JSON value in the last workshop.

[00:00:26] Yeah, this is a good point, and what I will say is that the bar is pretty high for including something in TypeScript itself. So we have, for example, Extract and Exclude, these are core types that are already in TypeScript. What I will say is there's nothing stopping you from writing your own types as a little library and importing that in the projects that you care about.

[00:00:57] For example, if I go to, how do I spell my name again? [LAUGH] Mike-north/types, you can see I have a little git repo here that is types, interfaces, and classes that I often use. And look, there's ConstructorArgs, so I bring this package into a variety of things that I work on.

[00:01:26] And some of these have made it into the language already, like at the time I wrote this library there was no required and there was no make everything optional, but I made my own. And as a result they're still here and that's fine, I'm not under pressure to get rid of these because these don't add to asset size, right, they just compile straight out, it's an identical type to what they have built.

[00:01:57] If we click that you can see that, well, we'll look more into this but It's just type information in a little JavaScript library. And if you would look at what happens with this kind of library when it's all bundled up, with one tiny exception here, so I create something called a deferred which is actually a class, right, so this isn't purely type information, a lot of this library is, right.

[00:02:22] But here, this is kinda like an inverted promise where whoever is holding the promise, they get to call resolve or reject from the outside, so it's gonna add a tiny little bit of asset size but nothing to be concerned about. So that's how I would answer this question, as you find things that are useful package them up and make them into little libraries that you can consume.

[00:02:52] I have one called JSON-typescript, and if we look at what this library contains it's very, very similar to what we built in the fundamentals class, right, this is an old school way that involves types and interfaces so this is compatible with TypeScript 2.8 even. I'm doing the same thing that we did in the fundamentals class, so all I have to do is install this package in a project and I can import stuff from it just as if it were a little library.

[00:03:22] If you wanna know how to set these kinds of libraries up robustly, and by that I mean if we look at this mike-north/types thing and the CI pipeline I have going here, I believe I'm testing against multiple nodes, like multiple versions of Node JS and multiple versions of the TypeScript compiler.

[00:03:44] And this means that as new dependencies are added, if you have dependencies here you're making sure that everything still continues to work in a very complete way. So this would be like I would be flattered and proud if you were to copy the infrastructure here and to use this for your own types, right, you just take everything in this source folder and modify it or add to it as you see fit.

[00:04:12] And feel free to open pull requests into mine if you think mine's useful and you wanna build on that instead, but this is sort of my own little mini standard library if you will. So we have three things ahead of us. Two more chapters in the website, and then I'm going to try to put everything kind of together in accomplishing a non-trivial task in the TypeScript playground, and that's gonna be building a little mini data layer.

[00:04:40] Because when we have different types of records, we have a lot of well structured type information and it's a place where we can take advantage of some of the things we've learned for high code reuse. So that's what we have ahead of us.