How to fix Type 'X' is not exported from type definition file in TypeScript

TypeScriptINTERMEDIATEMEDIUMUpdated Dec 11, 2025ยท 126 views

This error occurs when you try to import a type that exists in a library but is not explicitly exported in its declaration file. It typically happens with module resolution Node16/NodeNext or when types are marked as private.

Quick fix

Fixes Type 'X' is not exported from type definition file

# Look at the declaration file in node_modules
cat node_modules/library-name/index.d.ts

# Or check @types package if applicable
cat node_modules/@types/library-name/index.d.ts
  1. Check what types are actually exported
  2. Use utility types to infer the type
  3. Create a custom type definition
  4. Adjust module resolution settings
typescript โ€” error
# Look at the declaration file in node_modules
cat node_modules/library-name/index.d.ts
# Or check @types package if applicable
cat node_modules/@types/library-name/index.d.ts
Type 'X' is not exported from type definition file

What this error means

This TypeScript error indicates that you're attempting to import or reference a type that exists within a library's type definition file (.d.ts) but hasn't been explicitly exported for external use. This commonly occurs when library authors intentionally keep certain types private or when using stricter module resolution strategies like "Node16" or "NodeNext" that respect package.json "exports" fields. TypeScript declaration files can contain types that are used internally but not meant to be part of the public API. When you try to access these non-exported types, TypeScript blocks the import to maintain proper encapsulation. This is especially common in third-party libraries where the maintainers control what types are exposed to consumers. The error became more prominent after TypeScript 3.9, which changed how exports from .d.ts files are handled, making type visibility more strict and aligned with JavaScript module semantics.

Typical symptoms

  • Import statement fails with error about type not being exported
  • Type exists in node_modules/@types but cannot be imported
  • IntelliSense shows the type in the declaration file but import fails
  • Error only appears with Node16/NodeNext module resolution
  • Type worked in older TypeScript versions but fails after upgrade

Common causes

  1. Type exists in declaration file but lacks an export statement
  2. Using Node16/NodeNext module resolution with package.json exports that exclude the type
  3. Library intentionally keeps the type private as an internal implementation detail
  4. Attempting to import from an internal module path rather than the main entry point
  5. TypeScript version upgrade changed export behavior (3.9+)
  6. Type is declared locally in .d.ts file without export modifier

How to fix "Type 'X' is not exported from type definition file"

1Check what types are actually exported

Examine the library's declaration file to see what's publicly available:

bash
# Look at the declaration file in node_modules
cat node_modules/library-name/index.d.ts

# Or check @types package if applicable
cat node_modules/@types/library-name/index.d.ts

Look for export statements to identify publicly available types. If the type you need isn't exported, it's intentionally private.

2Use utility types to infer the type

Extract the type from exported functions or values using TypeScript utility types:

typescript
import { someFunction, someValue } from 'library-name';

// Infer return type
type MyType = ReturnType<typeof someFunction>;

// Infer parameter type
type ParamType = Parameters<typeof someFunction>[0];

// Infer type from a value
type ValueType = typeof someValue;

This approach lets you derive the type without requiring it to be explicitly exported.

3Create a custom type definition

If you need the exact type structure, recreate it in your own codebase:

typescript
// types/custom.d.ts
export interface MyCustomType {
  // Mirror the structure from the library's source
  id: string;
  name: string;
  // ... other properties
}

Base this on the library's documentation or source code. This gives you type safety without relying on private exports.

4Adjust module resolution settings

If the error only appears with strict module resolution, consider adjusting tsconfig.json:

json
{
  "compilerOptions": {
    // Change from Node16/NodeNext to bundler or node
    "moduleResolution": "bundler",
    // Or use the classic node resolution
    "moduleResolution": "node"
  }
}

Note: This is a workaround. Node16/NodeNext provides better alignment with modern Node.js module behavior.

5Import from alternative paths

Some libraries export types from different entry points:

typescript
// Instead of importing from the main entry
// import { InternalType } from 'library-name';

// Try importing from specific subpaths
import { InternalType } from 'library-name/types';
import { InternalType } from 'library-name/dist/types';

Check the library's package.json "exports" field to see available paths.

6Request the type be exported

If you have a legitimate use case for the type, open an issue with the library maintainers:

markdown
## Request: Export [TypeName] type

**Use case:** Describe why you need this type exported

**Current workaround:** Explain how you're working around it

**Impact:** How this affects type safety in your application

Many maintainers will export types if there's clear community demand.

Advanced notes

Declaration File Behavior Changes

TypeScript 3.9 changed how exports in .d.ts files are handled. Before 3.9, all types in declaration files were implicitly available. After 3.9, only explicitly exported types are accessible, aligning with JavaScript module semantics.

Package.json Exports and Type Visibility

With Node16/NodeNext module resolution, TypeScript respects the "exports" field in package.json. Even if a type exists in a .d.ts file, it won't be accessible unless the path is listed in exports:

json
{
  "exports": {
    ".": "./dist/index.js",
    "./types": "./dist/types.d.ts"  // Makes types path accessible
  }
}

Ambient vs Module Declarations

Types declared in ambient declaration files (no imports/exports) behave differently from module declaration files (with imports/exports). In module .d.ts files, you must explicitly export types for them to be available.

Type-only Imports

When importing types that are exported, use type-only imports to ensure they're stripped during compilation:

typescript
import type { MyType } from 'library-name';

This prevents runtime errors if the library only exports types without corresponding values.

Frequently asked questions

What does "Type 'X' is not exported from type definition file" mean?
ts) but hasn't been explicitly exported for external use. json "exports" fields. TypeScript declaration files can contain types that are used internally but not meant to be part of the public API.
How do I fix "Type 'X' is not exported from type definition file"?
This error occurs when you try to import a type that exists in a library but is not explicitly exported in its declaration file. It typically happens with module resolution Node16/NodeNext or when types are marked as private.
What causes "Type 'X' is not exported from type definition file"?
Type exists in declaration file but lacks an export statement Using Node16/NodeNext module resolution with package.json exports that exclude the type Library intentionally keeps the type private as an internal implementation detail
Is the "Type 'X' is not exported from type definition file" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading