Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't replace .d.ts paths #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

LukeAbby
Copy link

Context: I have an alias written as "@utils": ["./src/utils/index.d.mts"] and it was being changed to ./src/utils/index.d.mjs which broke resolution.

I know it's obviously niche to be using tsc-alias on already written .d.ts files but I'm of course typing someone else's code so plain .ts files don't make sense. I can see a few alternatives here:

  1. Put this behind a flag noRewritePaths or something.
  2. Test both .d.mts and .d.mjs
  3. Just don't support this weird use case.

But I think this solution is pretty simple!

Comment on lines +86 to +88
const dotIndex = base.indexOf('.');
const name = base.slice(0, dotIndex);
const extension = base.slice(dotIndex);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might wonder why I did this instead of just using the extension already given to me by parse. This is because they consider xyz.d.ts to have a name of xyz.d and an extension of .ts

const extension = base.slice(dotIndex);

const normalizedExtension = extension.replace(
/^\.([mc])?ts(x)?$/,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a ^ which means the whole extension must be matched. This may need to have smarter logic.

Files to definitely exclude:

  • foo.d.css.ts - The correct way of defining a declaration file for foo.css file. Cannot point to a js file.
  • foo.css.d.ts - A legacy way of defining a foo.css file. Cannot point to a js file.
  • foo.d.ts - Declaration files for a JS file. This JS file could technically exist in repo but if it does the .d.ts file should probably exist alongside. Also if you're doing this you probably should have a plain .ts file instead.
  • foo.xyz.ts - I'm unsure why you'd write this but it would be broken by this regex change. Maybe some libraries make this common or some repos do this?

If you'd like me to look into a better way, I can.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided I may as well push a fix for foo.xyz.ts. See "Add explicit isDTS detection"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant