TypeScript Declaration File Resolver

2.1.3 · active · verified Sun Apr 19

dts-resolver is a utility library designed to accurately locate and resolve TypeScript declaration files (.d.ts) for JavaScript and TypeScript dependencies within a project. It leverages the high-performance `oxc-resolver` engine to efficiently navigate `node_modules` and package `exports` maps, providing correct paths to type definitions. The current stable version is 2.1.3. The package has a moderately active release cadence, frequently incorporating bug fixes and performance improvements, and keeping up with its underlying resolver. Its key differentiator is its reliance on `oxc-resolver`, offering a robust and modern approach to declaration file resolution, crucial for build tools, IDEs, and other development workflows that need to understand the type landscape of a project's dependencies.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize `dts-resolver` and resolve the TypeScript declaration file path for a specified npm package.

import { createResolver } from 'dts-resolver';
import path from 'node:path';

async function resolveTypesForPackage(packageName: string) {
  const resolver = createResolver({
    cwd: process.cwd(), // Define the current working directory for resolution
    // You can add custom conditions if your project uses them, e.g., ['node', 'require', 'types']
    // conditions: ['node', 'require', 'types'],
    // Optionally disable resolving node modules if you only want local files, though usually not desired for dts resolution
    // resolveNodeModules: true,
  });

  try {
    const declarationPath = resolver(packageName);
    if (declarationPath) {
      console.log(`Resolved declaration for '${packageName}': ${declarationPath}`);
      console.log(`Located in directory: ${path.dirname(declarationPath)}`);
    } else {
      console.log(`Could not resolve declaration for '${packageName}'.`);
    }
  } catch (error) {
    console.error(`Error resolving '${packageName}':`, error);
  }
}

// Example usage for a common package
resolveTypesForPackage('fast-glob');
// Example for a package that might not have direct types or different structure
resolveTypesForPackage('lodash');

view raw JSON →