unplugin-dts icon indicating copy to clipboard operation
unplugin-dts copied to clipboard

RollupTypes: true wrong imports ([Type]_2)

Open nicolaeClichici opened this issue 1 year ago • 2 comments

Describe the bug

Hello, I'm getting import errors in file d.ts after build with option rollupTypes: true. As you can see bellow, in d.ts file is redundant import of User interface.

Reproduction

Steps to reproduce

Project structure: image Vite config:

export default defineConfig({
  cacheDir: '../../node_modules/.vite',
  plugins: [
    react(),
    tsconfigPaths(),
    dts({
      tsconfigPath: resolve(__dirname, 'tsconfig.app.json'),
      rollupTypes: true,
    }),
  ],
  build: {
    minify: false,
    sourcemap: true,
    emptyOutDir: true,
    lib: {
      entry: entries,
      name: 'UI',
      formats: ['es'],
      fileName: (_, entryName) => `${entryName}.js`,
    },
    rollupOptions: {
      external: [
        'react',
        'react-dom',
        'react/jsx-runtime',
      ],
      output: {
        inlineDynamicImports: false,
        preserveModules: true,
        globals: {
          react: 'React',
          'react/jsx-runtime': 'react/jsx-runtime',
          'react-dom': 'ReactDOM',
        },
      },
    },
  },
});

package.json:

{
  "name": "dts-test",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc -b && vite build",
    "lint": "eslint .",
    "preview": "vite preview"
  },
  "dependencies": {
    "react": "^18.3.1",
    "react-dom": "^18.3.1"
  },
  "devDependencies": {
    "@eslint/js": "^9.17.0",
    "@types/react": "^18.3.18",
    "@types/react-dom": "^18.3.5",
    "@types/node": "^22.10.1",
    "@vitejs/plugin-react": "^4.3.4",
    "eslint": "^9.17.0",
    "eslint-plugin-react-hooks": "^5.0.0",
    "eslint-plugin-react-refresh": "^0.4.16",
    "globals": "^15.14.0",
    "typescript": "^5.7.2",
    "typescript-eslint": "^8.18.2",
    "vite": "^6.0.5",
    "vite-plugin-dts": "4.5.0",
    "vite-tsconfig-paths": "^5.1.4"
  },
  "files": [
    "dist"
  ],
  "main": "./dist/index.js",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "import": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.js"
      }
    }
  }
}

get-user.ts:

import { User } from "./types.ts"

export const getUser = (): User => {
  return {
    name: 'test',
    surname: 'test'
  }
}

some-util.ts:

import {getUser} from "./get-user.ts";

export const someUtil = () => {
  return getUser()
}

types.ts:

export interface User {
  name: string;
  surname: string;
}

(After build) index.d.ts:

import { User as User_2 } from '../types.ts';

export declare const getUser: () => User;

export declare const someUtil: () => User_2;

export declare interface User {
    name: string;
    surname: string;
}

export { }

System Info

System:
    OS: macOS 14.4
    CPU: (11) arm64 Apple M3 Pro
    Memory: 702.64 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
Binaries:
    Node: 23.3.0 - /opt/homebrew/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.9.0 - /opt/homebrew/bin/npm
Browsers:
    Chrome: 131.0.6778.265
    Safari: 17.4
npmPackages:
    @vitejs/plugin-react: ^4.3.4 => 4.3.4 
    vite: ^6.0.5 => 6.0.7 
    vite-plugin-dts: 4.5.0 => 4.5.0

Validations

nicolaeClichici avatar Jan 13 '25 08:01 nicolaeClichici

This looks very similar to an issue that I have reported as microsoft/rushstack#5126 and that also seems to be tracked as microsoft/rushstack#5106. As a workaround, downgrade @microsoft/api-extractor to 7.48.1. For yarn, specify this in your package.json (for yarn workspaces, specify it in the root package):

  "resolutions": {
    "@microsoft/api-extractor": "7.48.1"
  }

Another workaround would be to use moduleResolution: "nodenext" in the tsconfig, but that does not seem to work because of #417.

cdauth avatar Mar 01 '25 15:03 cdauth

I get a similar issue that even still happens when downgrading @microsoft/api-extractor to 7.48.1:

export declare interface AppStateOutput {
   // ... properties
}

declare interface AppStateOutput_2 {
   // ... properties
}

This only happens when I export types in the index.ts file. When I don't export them, there are no duplicate types. This happens to all types that are defined in the separate types.ts file. As I need to use those types outside of the package, it is not really acceptable to not export them.

tchock avatar Aug 15 '25 13:08 tchock