TypeScript SDK Development: A 5-Year-Old Could Follow This Step-By-Step ~ Part 1: Our First MVP | HackerNoon

Helloooooooo!

Hope you’re doing great! This is SMY! šŸ‘‹ Let’s jump right in šŸš€

Source Code:Ā https://github.com/smyaseen/typescript-sdk-template

Contents:

  • āš”Ā Some Background of SDK Development
  • āš”Ā Developing and running our first version

1ļøāƒ£ What –

SDK (sometimes also known as library) serves as a plug-in in applications to derive additional features from the technology.

2ļøāƒ£ Why –

SDK development with TypeScript offers reliability over a long time due to type safety and maintenance in the long run.

3ļøāƒ£ How –

The fundamental steps required to build the SDK are the following:

  1. Initializing the project with the right workflow settings.
  2. Choosing Bundler, and understanding its purpose.
  3. Understanding ESM, CJS, IIFE UMD to run SDK on different environments.
  4. Publishing as a library on NPM, semantic versioning, and License.
  5. NPM for SPAs and CDN for Browsers.

In Part 1, we are going to build our first basic SDK to get a basic understanding.

Step 1: Initialize Project

Run the following command to set the project in a new folder:

npm init -y

"-y"Ā defaults to yes to all the follow-up prompts. You can change it later in the Package.json like the author, license, version, etc.

Head over toĀ package.json,Ā and addĀ type: moduleĀ to work with the latest EcmaScript Module system (ESM).

YourĀ package.jsonĀ should look like the following:

 {
  "name": "ts-lib",
  "version": "1.0.0",
  "description": "SDK development tutorial",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Step 2: Install Fundamental Dev Libraries

  1. TypeScript
  2. @types/nodeĀ – to work TypeScript with NodeJS.
  3. tsupĀ – The simplest and fastest way to bundle your TypeScript libraries.

COPY

npm i typescript @types/node tsup -D

Step 3: SetupĀ tsconfigĀ for TypeScript Settings

Create aĀ tsconfig.jsonĀ file at the root of the project.

COPY

touch tsconfig.json

Head over to the file, and paste the following configuration:

{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "allowImportingTsExtensions": true,
    "emitDeclarationOnly": true,
    "skipLibCheck": true,
    "target": "es2022",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,
    "verbatimModuleSyntax": true,

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,

    /* If transpiling with TypeScript: */
    "module": "NodeNext",
    "sourceMap": true,
    "outDir": "dist",

    /* AND if you're building for a library: */
    "declaration": true,

    /* If your code runs in the DOM: */
    "lib": ["es2022", "dom", "dom.iterable"]
  },
  "exclude": ["node_modules", "dist"]
}

You can hover over each property to learn more in-depth about it.

The fundamental thing to understand here is:

    "module": "NodeNext",
    "sourceMap": true,
    "outDir": "dist",
  1. ”Ā NodeNextĀ is the right option for authoring libraries because it prevents you from emitting ESM with module specifiers thatĀ onlyĀ work in bundlers but will crash in Node.js. When writing conventional code, using common sense, and relying on high-quality dependencies, its output is usually highly compatible with bundlers and other runtimes.” You can learn more about it here:Ā https://blog.andrewbran.ch/is-nodenext-right-for-libraries-that-dont-target-node-js/

  2. sourceMapĀ – Enables the generation of source files. These files allow debuggers and other tools to display the original TypeScript source code when working with the emitted JavaScript files. You can disable it for production.

  3. outDirĀ – Specify an output folder for all emitted files.

  /* AND if you're building for a library: */
    "declaration": true,

    /* If your code runs in the DOM: */
    "lib": ["es2022", "dom", "dom.iterable"]
  1. declarationĀ – Generate .d.ts files from TypeScript and JavaScript files in your project.

  2. libĀ – Specify a set of bundled library declaration files that describe the target runtime environment.Ā es2022Ā is for node applications like React, andĀ domĀ &Ā dom.iterableĀ for running the library in the browser.

Step 4: Write Our First Code

Create aĀ index.tsĀ file, and write the following basic code:

const add = (a: number, b: number): number => a + b;
const subtract = (a: number, b: number): number => a - b;

export { add, subtract };

Build our first code:

COPY

tsup ./index.ts

You may now see we have aĀ distĀ folder with an output fileĀ index.cjs

Let’s integrate and run our first SDK!

Create aĀ app.jsĀ file, and paste the following code:

import { add, subtract } from "./dist/index.cjs";

console.log(add(1, 2));
console.log(subtract(2, 1));

Since we haven’t published our SDK, we are directly linking with local build.

Now, run our first app

node app.js

You should see the following output:

3
1

Congrats šŸŽ‰šŸ„³ šŸš€šŸš€šŸš€ We just built and ran our first SDK!

Wrapping Up:

We Just completed the basic steps to build and run our first SDK. Head over to Part 2, where we will build a basic folder structure, and integrate an External API endpoint šŸš€

…..

Now, you’re equipped with knowledge to build your own SDK. Happy coding! šŸš€

That’s it, folks! Hope it was a good read for you. Thank you! āœØ

šŸ‘‰ Follow me

GitHub

LinkedIn