0
0
Typescriptprogramming~3 mins

Why Augmenting third-party libraries in Typescript? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could improve any library without breaking it or losing your changes when it updates?

The Scenario

Imagine you are using a library in your project, but it lacks some features you need. You try to add those features by copying and changing the library code manually. This is like rewriting parts of a big instruction manual every time you want a small change.

The Problem

Manually changing library code is slow and risky. You might break something without knowing. Also, when the library updates, you have to redo your changes all over again. This wastes time and causes frustration.

The Solution

Augmenting third-party libraries lets you add or change features safely without touching the original code. You extend the library in your own code, so updates don't erase your improvements. It's like adding sticky notes to a book instead of rewriting pages.

Before vs After
Before
import lib from 'library';
// copy library code and modify it here
function newFeature() { /* copied and changed code */ }
After
import 'library';
declare module 'library' {
  interface ExistingInterface {
    newFeature(): void;
  }
}
// now you can use newFeature safely
What It Enables

You can safely customize and improve libraries to fit your needs without breaking updates or risking bugs.

Real Life Example

Suppose you use a date library that doesn't support your local holiday calendar. By augmenting it, you add holiday checks directly to the library's date objects, making your app smarter without rewriting the whole library.

Key Takeaways

Manual changes to libraries are risky and hard to maintain.

Augmenting adds features safely without touching original code.

This keeps your project stable and easy to update.