This lesson has been retired.

Video Processing Status: completed

No video source files were found yet. Check back in an hour. If there's still a problem let us know.

Become a member
to unlock all features

    TypeScript - Generating Definition Files

    John LindquistJohn Lindquist

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:00 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.

    00:24 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.

    00:40 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."

    00:52 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.

    01:09 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.

    01:33 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.

    01:48 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.

    02:13 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.

    02:34 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.