Nacho's Blog

Adding Types to DefinitelyTyped

As any good typescript developer, we all love to just import the types for our libraries from DefinitelyTyped as if it was magic, but it's not. It's an open source library that's maintained by contributors. So this is also a little peek on how to contribute to an open source project.

1. Making out type file

This is the part where you need to use your knowledge of typescript and the library to actually write the type definitions. In order to test it, the best way is to move the folder into the @types folder within node_modules. This is the actual place the definitions will be when imported by another user.

Types should be in an index.d.ts file all within a folder named the same as the module being typed, or the name followed by "-browser" if it's not a NPM module. Remember to declare the module like this:

declare module "name" {
    // Types here

2. Adding tsconfig, tslint and Header

Tsconfig and tslint are mostly expected to just be unchanged from the documentation, but writing types for a non NPM package will force you to turn off some linter rules and cause the bot to report that as an error.

The headers are structured comments at the top of your index.d.ts file, and these are actually required. These are also used for reference and ownership attribution, so it's good that you can't forget them. They should be at the very top of the file and look like this:

// Type definitions for name 1.0
// Project: https://github.com/library-name
// Definitions by: Your Name <https://github.com/username>
//                 Colaborator's Name <https://github.com/username>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

It is appreciated (by anyone who uses JS) if your type definitions also include JSDoc annotations for people to see later, so remember to write those.

At this point, you can also add a readme.md file if you want.

3. Cloining the repo

This part is easy, just fork DefinitelyTyped and clone the new repo.

4. Adding tests

The files should be tested, and the tests should cover all defined methods. The testing is done with dtslint, this library allows you to just call methods and variables and assert their type. In order to do that, two assertions are used:

// $ExpectType void
// @ts-expect-error

One is for asserting expected type and the other to assert an expected type error.

5. Making the PR

First merge your code int othe main branch of your fork of DefinitelyTyped. Then go to DefinitelyTyped and create a new PR from your fork's main into DefinitelyTyped's main. For this particular project, PR's have a handy template. But if that wasn't the case, remember to write a good PR description.

6. Merging

Now you have to wait for CI to pass and wait for a member's approval. This takes a day or two. Once approved by a member, you can just make a comment with "Ready to merge" and the bot will automatically merge it.


All in all. The maintainers were super helpful and the contribution process for DefinitelyTyped is very painless.

Let's get typing!

Published: Tuesday, Sep 5, 2023
Privacy Policy© 2023 Ignacio Degregori. All rights Reserved.