This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

TypeScript - Generating Definition Files

2:41 TypeScript lesson by

TypeScript allows you to generate definition files for your own libraries. This lesson shows you how to organize your project and generate the definition files so that others projects can use your library with TypeScript.


egghead.io comment guidelines

Avatar
egghead.io

TypeScript allows you to generate definition files for your own libraries. This lesson shows you how to organize your project and generate the definition files so that others projects can use your library with TypeScript.

Avatar
Cosmin

Great course! It would be very helpful to have a lesson that covers the subject of importing js files into ts files with systemjs, by going through the process of manually writing the definition file. Thanks!

In reply to egghead.io
Avatar

Wonderful tutorial ! I learn lot, specially last two courses, for definition , typings and loading modules, I always have no idea how to use those systemjs, typings, definetions, index.ts, index.d.ts, and no any articles about those.

If you're writing a library and you want to generate your own definition files, just make sure and add declaration to your tsconfig, set that to true. Then, anytime it compiles, we'll hop into our dist. You can see that we now have a main.d.ts and a socialnetwork.d.ts, and that it created those files for us. Anyone who included our TS lessons library would be able to reference these.

An error that will show up sometimes, it'll say you'll have a duplicate definition of app in here and here, so you want to make sure in your tsconfig you've excluded dist, so that you avoid having these duplicate definitions.

Otherwise, it'll say, "I found app both here and in the source directory, and I don't know which one to use." Excluding it in the tsconfig is saying, "The good stuff doesn't belong in my source, so ignore it."

The way our project stands now is, if someone were to...I'm just going to write this in line. If someone were to use our project, our ts lessons project, they'd have to import ts lessons and then import main, because there's a main.d.ts and that's where they'd get it from.

Often, what you want to do is say import ts lessons and then get app off of it. Similarly, they'd also want to get person and social network and all these things that are defined across both d.ts and socialnetwork.ts in that same sort of package of our library. What you do for that is you create a new source file.

We'll call this index.ts. In the index, you can export the different files. Export everything from main and export everything from social network.

That way, it's going to allow people to just import this as the ts lessons, and then grab anything off of what they want. You'll see this is just the exact same file but as a definition file. Then, when someone wanted to import something off of ts lessons, they could do the grab, app, person, social network, and on and on, because we created this index file for them.

What you can do for typing is in your package JSON, we'll just say that the typing for our file is in that distindex.d.ts, that way, when that package gets added to typing and someone loads it up, they'll get this index file, and then they'll have access to everything off of how we structured our library.

From here, you can expose anything you want, or even exclude or by simply not referring to the things that you want to export the API of your library.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?