0Pricing
TypeScript Academy · Lesson

Dealing with Untyped Third-Party Libraries

Use @types packages and write manual declarations.

Dealing with Untyped Third-Party Libraries is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Problem: Missing Types

Some npm packages do not ship TypeScript declarations and have no @types/ package. TypeScript treats them as any by default, losing type safety at the boundary.

import legacyLib from "untyped-lib"; // legacyLib: any

@types Packages

The DefinitelyTyped project provides community-maintained type declarations for thousands of libraries. Search npm for @types/library-name.

npm install --save-dev @types/lodash
npm install --save-dev @types/express
# Now lodash and express have full TypeScript types

Writing a Declaration File (.d.ts)

If no @types package exists, write a minimal declaration file in your project to declare the module and its types.

// src/types/untyped-lib.d.ts
declare module "untyped-lib" {
  export function doSomething(x: string): number;
  export const version: string;
}

Module Augmentation

Extend existing third-party types by adding your own declarations to an existing module without forking it.

// Extend Express Request with a custom property
declare namespace Express {
  interface Request {
    user?: AuthUser;
  }
}

Wildcard Module Declarations

For entire categories of untyped imports (e.g., asset files), use wildcard module declarations.

// src/types/assets.d.ts
declare module "*.svg" {
  const content: string;
  export default content;
}
declare module "*.json" {
  const value: Record<string, unknown>;
  export default value;
}

noImplicitAny and Untyped Libraries

With noImplicitAny: true, importing an untyped module causes a compile error. Use a declaration file or disable noImplicitAny per file with // @ts-ignore.

// Quick fix for a single untyped import:
// @ts-ignore
import untypedLib from "untyped-lib";

Contributing to DefinitelyTyped

If you write high-quality types for an untyped library, submit them to DefinitelyTyped to help the community.

# Fork DefinitelyTyped and add:
# types/your-library/index.d.ts
# types/your-library/package.json
# Submit a PR at github.com/DefinitelyTyped/DefinitelyTyped

Using any as a Last Resort

When you cannot get types quickly, use explicit any with a comment tracking the debt. This is better than implicit any because it is intentional and visible.

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const lib: any = require("untyped-lib"); // TODO: add types

skipLibCheck for Declaration Errors

If a third-party .d.ts file has internal errors, skipLibCheck: true silences them without affecting your own type checking.

{
  "compilerOptions": {
    "skipLibCheck": true
  }
}

Type Wrappers for Unsafe Libraries

Write a typed wrapper module around an untyped library to contain the any and expose a safe typed API to the rest of your codebase.

// src/lib/safe-legacy.ts
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const raw: any = require("untyped-lib");
export function doSomething(x: string): number { return raw.doSomething(x); }

Recap: Untyped Libraries

Handle untyped libraries: check @types/ packages first, write minimal declaration files, use module augmentation, create typed wrappers, and use skipLibCheck for declaration errors in dependencies.

Quick Check

What is the first place to look for type declarations for an untyped npm package?

What You Learned

Untyped libraries: check @types/ packages, write declaration files, augment existing types, use wildcard module declarations, wrap unsafe libraries in typed facades, and use skipLibCheck for dependency errors.

Frequently asked questions

Is the “Dealing with Untyped Third-Party Libraries” lesson free?

Yes — the full text of “Dealing with Untyped Third-Party Libraries” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Dealing with Untyped Third-Party Libraries”?

Use @types packages and write manual declarations. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Dealing with Untyped Third-Party Libraries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Starting the Migration: allowJs and checkJs
  2. JSDoc Type Annotations as a Bridge
  3. File-by-File Conversion Strategy
  4. Dealing with Untyped Third-Party Libraries
← Back to TypeScript Academy