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