Skip to content

TypeScript types break when aliasing the package #17520

@sxzz

Description

@sxzz

Describe the bug

When installing Svelte via an alias in package.json, TypeScript type resolution fails.

// package.json
"dependencies": {
   "svelte5": "npm:svelte@5"
}

This happens because Svelte currently relies on ambient module declarations in a single dts file (e.g., declare module 'svelte/compiler' { ... }). These hardcoded module names do not automatically adjust to the package alias (e.g., svelte5/compiler).

Furthermore, build tools like tsdown or tsup cannot correctly analyze these ambient types for bundling or dts generation.

Suggestion

I suggest moving away from the monolithic declare module approach. A more modern and compatible structure would be to use separate .d.ts files alongside their respective entry points.

Current Structure (simplified):

// types/index.d.ts
declare module 'svelte/compiler' { /* ... */ }
declare module 'svelte/action' { /* ... */ }

Proposed Structure:

dist/
  index.js
  index.d.ts
  compiler.js
  compiler.d.ts

This change would allow TypeScript to resolve types relative to the file location, making the package agnostic to its installed name.

Relate issue: sxzz/rolldown-plugin-dts#170

Reproduction

  1. Create a new project.
  2. Install Svelte with an alias: pnpm install svelte5@npm:svelte@5.
  3. Try to import from the compiler:
    import {} from 'svelte5'
  4. Observation: TypeScript throws an error: File '/proj/node_modules/.pnpm/svelte@5.48.0/node_modules/svelte/types/index.d.ts' is not a module

Logs

System Info

N/A

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions