Skip to content

Change declaration file import paths from .js to .d.ts #50629

Open
@frehner

Description

@frehner

Suggestion

🔍 Search Terms

  • declaration import paths
  • esnext declaration import path

I also know there's a lot of somewhat-related conversation around this area in #37582 and #46452 , but this is only for the output of declaration files, so I'm not sure if it's exactly related to those issues or not. If it is, feel free to close this and accept my apologies.

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

When using TypeScript to emitDeclarationOnly, it outputs a .d.ts file. However, the contents of that file keep the same import path. An example:

// in `index.d.ts`
export {TestFunc} from './other.js';

What would be nice, however, is to have the declaration file import paths point to the actual file, like so:

// in `index.d.ts`
export {TestFunc} from './other.d.ts';

To maybe help see more clearly, I created this repo that has the current source code and output files when running npx tsc --emitDeclarationOnly. You'll note that the /types/index.d.ts file has an import/export path to a file that doesn't actually exist, but is still found when using something like node's resolution algorithm.

📃 Motivating Example

When using Deno, you can put up your library + types up on a CDN. However, Deno must have the exact path to the file in order for it to work. Unfortunately it seems that it means that the other declaration files cannot be found because the import path in the declaration file isn't exactly correct.

Unless I've missed something important about TS / Deno?

💻 Use Cases

I don't think this would have any effects on the ecosystem, would it? Is there a chance it would maybe even make things slightly faster when looking up types (even for node projects), because the declaration files are pointing directly to the correct file instead of having to go through the resolution algorithm? I'm not sure, I'll defer to you experts.

Thank you ❤️

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: Declaration EmitThe issue relates to the emission of d.ts filesSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions