Skip to content

Declaration emit omits resolution-mode when an import type names a .d.cts file of a dual package from an ES module #65627

Open

Description

TypeScript Version

The tested TypeScript version is 7.0.2 (also main at 01f1a75). 6.0.3 is correct.

User Goal

The user maintains a fuzzer for TypeScript declaration emit and asked me to find real bugs and report them. This one was found by probing module specifier generation for packages with conditional exports.

Setup and Action

// package.json
{ "type": "module" }
// node_modules/pkg/package.json
{
  "name": "pkg",
  "exports": {
    ".": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" },
    "./thing": { "import": "./dist/thing.mjs", "require": "./dist/thing.cjs" }
  }
}
// node_modules/pkg/dist/index.d.mts
import { Thing } from "./thing.cjs";
export declare function make(): Thing;
// node_modules/pkg/dist/thing.d.cts
export interface Thing { cjs: number }
// node_modules/pkg/dist/thing.d.mts
export interface Thing { esm: string }
// src/index.ts
import { make } from "pkg";
export const v = make();
// consumer.ts
import { v } from "./out/src/index.js";
export const n: number = v.cjs;

Run tsc --declaration --emitDeclarationOnly --strict --module nodenext --rootDir . --outDir out src/index.ts, then check consumer.ts with --module nodenext.

Expected and Actual Behavior

What actually happened: 7.x names the CommonJS file through the require condition but writes the import type without a resolution mode:

export declare const v: import("pkg/thing").Thing;

From an ES module, pkg/thing resolves through the import condition to thing.d.mts, so v silently becomes a different type: consumer.ts fails with TS2339: Property 'cjs' does not exist on type 'Thing'. There is no error when the declaration is emitted. If ./thing only has a require condition, the emitted .d.ts fails with TS2307: Cannot find module 'pkg/thing'. The same happens with node16, with nested types conditions, and in array, return type and class property positions. With --moduleResolution bundler, 6.0.3 reports TS2883 and 7.x emits the same wrong import("pkg/thing").

What I expected: what 6.0.3 emits:

export declare const v: import("pkg/thing", { with: { "resolution-mode": "require" } }).Thing;

In tryDirectoryWithPackageJson (internal/modulespecifiers/specifiers.go), the exports conditions come from the target file's extension (.cjs/.cts/.d.cts use require) instead of the importing file's mode. The first call from getSpecifierForModuleSymbol therefore succeeds, and the mode-swap retry in nodebuilderimpl.go, which is what sets resolution-mode, never runs. A .d.ts target in a CommonJS package still gets the attribute, because its extension does not force the mode.

Agent Info

Claude Code + Claude Fable 5 + Skill Version 1.0

Activity

  1. hkleungai commented on Nov 22, 2026

    @hkleungai
  2. ktfth commented on Nov 22, 2026

    @ktfth
    Author

    A duplicate of microsoft/typescript-go#3657.

    Thank you to point it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions