Hey there! If you find this story helpful, feel free to give it a like or share it with someone who might benefit from it.
Like many others, I have a book collection, with hundreds of books in different genres and editions. Each book represents a JavaScript file, filled with stories and information. I love my collection, but I start to notice that some of the books have pages that are out of order, or even worse, missing altogether. To bring some order to this chaotic collection, I decide to introduce a new system: TypeScript.
TypeScript is like hiring a meticulous librarian who can gradually help me organize my collection. First, I don’t want to overwhelm my librarian by handing over all the books at once. Instead, I start with a single shelf. I select a manageable number of books that I think are the most important or the ones that need the most attention—these are my critical JavaScript files.
With these selected books, the librarian starts by checking each page for consistency and completeness. She places labels on them, ensuring every chapter follows a certain structure and that all references are clear. This process is akin to adding TypeScript type definitions to my JavaScript files, ensuring that functions have defined input and output types.
As the first shelf is beautifully organized, I gain confidence in my librarian’s abilities. I slowly begin to hand over more shelves, one by one, allowing her to apply the same meticulous care to the next batch of books. This gradual process is what I call “incremental adoption” of TypeScript. It allows me to maintain order while still enjoying my collection, without the overwhelming task of reorganizing everything at once.
Eventually, my entire collection is neatly organized. Each book is easy to navigate, and any new additions follow the same structured system. In this way, my librarian—TypeScript—has not only brought order and clarity to my collection but also made future expansions smooth and error-free.
Thanks for joining me on this journey! If you enjoyed the story, consider sharing it with fellow book lovers, or in this case, JavaScript developers.
Step 1: Initialize TypeScript
First, I need to introduce TypeScript into my project. I do this by running:
npm install typescript --save-dev
npx tsc --init
This sets up a tsconfig.json
file, which is like giving my librarian a set of rules and guidelines for organizing the books.
Step 2: Convert a Few Files
I start by converting a few of my JavaScript files to TypeScript. I choose files that are critical, much like my most valuable books. I rename them from .js
to .ts
. For instance, I take a simple file like calculate.js
:
function add(a, b) {
return a + b;
}
And convert it to calculate.ts
with type annotations:
function add(a: number, b: number): number {
return a + b;
}
This is similar to placing labels on my books, ensuring every function has clear input and output types.
Step 3: Incremental Adoption
I don’t want to convert everything at once, so I use the allowJs
option in my tsconfig.json
. This allows JavaScript files to coexist with TypeScript files, enabling me to slowly migrate more files over time:
{
"compilerOptions": {
"allowJs": true,
"checkJs": false, // If I want to avoid checking JS files initially
"outDir": "./dist",
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
Step 4: Leveraging @ts-check
For JavaScript files that I want to keep as JavaScript for now, I can still get TypeScript’s benefits by adding //@ts-check
at the top of the file:
// @ts-check
function multiply(a, b) {
return a * b;
}
This allows TypeScript to perform type-checking, acting as a safety net without fully converting the file.
Key Takeaways
- Incremental Adoption: Just like organizing a library one shelf at a time, I can adopt TypeScript gradually, starting with critical files.
- Type Safety: Adding type annotations improves code readability and helps catch errors early.
- Flexibility: Using
allowJs
and@ts-check
provides flexibility, allowing JavaScript and TypeScript to coexist during the transition. - Scalability: Once fully adopted, TypeScript helps maintain a scalable and robust codebase.