any from External Libraries Without Types
You'll learn: How importing third-party libraries without type definitions forces TypeScript to treat them as any, and what strategies exist to contain the damage.
The External Library Problem
When you import a third-party npm package that doesn't include TypeScript types (no .d.ts files) and doesn't have a corresponding @types/package-name on DefinitelyTyped, TypeScript has no choice but to treat everything from that module as any.
This is infectious — the moment you use anything from that library, you lose type safety. Values returned from its functions are any, objects have properties typed as any, and the entire contract between your code and the library becomes invisible to the compiler.
Think of it like receiving a package wrapped in completely opaque material — you have no idea what's inside until you open it at runtime.
Where This Happens
// Hypothetical library "cool-legacy-lib" with no types
import { processData } from 'cool-legacy-lib';
// processData is `any => any`
const result = processData({ value: 42 });
// result is `any` — total type safety loss
result.doesNotExist(); // No error!
Your IDE can't autocomplete, the compiler can't warn you, and refactoring becomes dangerous.
Containment Strategies
1. Create Your Own Declaration File
You can write a minimal .d.ts file that describes the parts of the library you actually use:
// cool-legacy-lib.d.ts
declare module 'cool-legacy-lib' {
export function processData(input: { value: number }): { result: string };
}
Now imports are typed, even if the library isn't.
2. Wrap in Typed Adapters
Create a thin typed wrapper around the untyped library, isolating the any to one module:
// adapter.ts
import { processData as rawProcess } from 'cool-legacy-lib';
export function processData(input: { value: number }): { result: string } {
return rawProcess(input);
}
The rest of your codebase imports from your adapter, never touching the raw library.
3. Use unknown at the Boundary
If you can't predict the library's return type, use unknown instead of letting any spread:
import { getData } from 'mystery-lib';
const data = getData() as unknown;
// Now you must narrow before use
Key Takeaway: Untyped third-party libraries introduce any at import boundaries. Contain the damage by writing declaration files, wrapping libraries in typed adapters, or using unknown to force validation before use.