Third-Party Untyped Libraries
What you'll learn: How to use any as a temporary escape hatch when working with external JavaScript libraries that lack type definitions.
The Real-World Problem
You're building a TypeScript project and want to use an excellent npm package that solves your problem perfectly. There's just one catch: it was written in plain JavaScript and has no type definitions—not in the package itself, and not in DefinitelyTyped either.
You now face a dilemma. You could write complete TypeScript declarations for the entire library, but that might take hours or days for a complex API. Sometimes the practical choice is to keep moving forward.
When any Becomes a Bridge
This is where any serves as a temporary bridge between the typed and untyped worlds. Instead of halting your project or spending days on declarations, you can import the library and type it as any:
// Suppose 'cool-untyped-lib' exists but has no types anywhere
const coolLib: any = require('cool-untyped-lib');
// Now you can call it without TypeScript errors
const result = coolLib.doSomething('hello');
// result is also typed as 'any'
You've lost type safety for this specific library, but you've kept your entire project compiling and moving forward.
The Analogy
Think of it like using a universal adapter when traveling. You have electrical devices designed for your home country (your typed code), but you encounter a foreign outlet (the untyped library). The universal adapter (any) isn't elegant and doesn't provide the safety of a proper native plug, but it gets power flowing so you can continue your trip.
When This Approach Makes Sense
Use any as a temporary bridge when:
- Writing full declarations would take longer than the value you're getting
- The library is simple and you understand its API from documentation
- You plan to replace the library later or contribute types eventually
- The untyped code is isolated to a small part of your codebase
This is a pragmatic compromise, not a best practice. The goal is always to have types, but any lets you ship today rather than getting blocked indefinitely.
Key Takeaway: When third-party JavaScript libraries lack types and writing declarations is impractical, any serves as a temporary bridge that trades type safety for forward progress.