The TypeScript compiler makes it super easy to create high quality NodeJS packages that can be used with compile time safety in other TypeScript packages.
Using JavaScript packages written in TypeScript can save you a lot of time digging around docs and then having to memorize the docs, in order to call the functions with the right spellings. This frees you up for a higher level of thinking and reasoning about logic and business requirements.
[00:00] We'll go ahead and initialize on note-package using npm in it selecting the default options. In order to create a TypeScript package, we need to install the TypeScript compiler. You'll install it using npm and save it to a devDependencies. Note that the compiler is completely self-contained with zero external dependencies.
[00:24] Now, we will go ahead and start our IDE in this folder. First, we'll create a tsconfig file for all types of configuration. We will set our compiler options with the declaration option set to true. The TypeScript compiler will automatically generate d.ts declaration files to allow seamless use of our package from other TypeScript projects.
[00:50] We will specify the output directory for all the JS and the declaration files to be the lib folder. Finally, we include all the files from the source folder to the compilation context. You set this dslfig as an active project in our IDE, and create the main index file in the source folder, which will be the entry point for our package.
[01:16] As a demonstration, we'll export simple sum function that sums two numbers. We'll go ahead and export the function called "Sum." That takes numbers A and B, and within the method body, it simply returns the sum of A and B. We on tsconfig, the only other thing we need to set up for TypeScript is our package to JSON.
[01:40] We will point the JS main to the compiled lib index file. Similarly, we will point the TypeScript, typings mean to be in the lib index file. Finally, we will set up a build target to compile Typescript to JavaScript using tsconfig with the TypeScript compiler. With this build target in place, we can compile our project by simply running npm run build.
[02:11] You can follow up this command with whatever process you currently use to publish your JavaScript npm packages. For example, I'll create a patch release and publish it to npm. Using such types of packages is super easy. You simply jump to another npm project, and install a newly published ts package as the dependency.
[02:34] Note that to use this package, we don't actually need Typescript. For example, we can jump to the Node Console, require the package, and use its exported members, all without using Typescript. By creating a package using Typescript, you're not limiting your target audience in anyway.
[02:52] That said, a package written in TypeScript shines even brighter for people that use TypeScript. Let's jump to our IDE and open a TypeScript file within this project. We can import a TypeScript module into this file.
[03:06] Notice the 100 percent reliable package name input. Also notice that you get a 100 percent reliable auto-complete when you import something from this module. We also get nice auto-complete for using the imported function.
[03:23] Quick info for the function is also available automatically for consumers of a module. Consumers also get type checking. For example, if you try to pass in a string instead of a number, you get a nice header.
[03:38] The result of the function call is also type checked for us. For example, we get quick info, we get auto-complete, all of this simply by using TypeScript for JavaScript node_modules.