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

Support path aliases for type checking (not for emitted code) #55432

Closed
5 tasks done
arturenault opened this issue Aug 18, 2023 · 2 comments
Closed
5 tasks done

Support path aliases for type checking (not for emitted code) #55432

arturenault opened this issue Aug 18, 2023 · 2 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@arturenault
Copy link

arturenault commented Aug 18, 2023

πŸ” Search Terms

baseUrl, paths, alias, resolution, type-checking

βœ… Viability Checklist

  • 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 our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

I've read the docs and open issues like #10866 and #16088. I understand that the baseUrl and paths options are designed to be used by bundlers, and do not affect code emitted by tsc.

I use esbuild and have gotten it to successfully transpile my code. However, esbuild (and many other bundlers) don't perform type checking and explicitly recommend using tsc --noEmit separately to do so.

But since tsc doesn't resolve relative to baseUrl or paths, type checking fails if I use paths and/or baseUrl, since those modules cannot be resolved.

πŸ“ƒ Motivating Example

Users of esbuild, swc, and shouldn't have to choose between the following two options:

Without type-checking

import { context } from "context"

With type-checking

import { context } from "../../../../context"

They should have the best of both worlds.

πŸ’» Use Cases

  1. What do you want to use this for?
    To write prettier code while keeping all the benefits of type-checking and esbuild.

  2. What shortcomings exist with current approaches?

  • Other module bundlers exist, but they're slow and lack features. Besides, this solution would mean we'd stop having to rely on module bundlers for type-checking.
  • module-alias paths are global, so it's hard to use in monorepo setups. Also only works in node.
  • tsc-alias only works with emitted code.
  1. What workarounds are you using in the meantime?
    Just writing ugly paths.
@RyanCavanaugh
Copy link
Member

Could you actually give an example of what you're doing in terms of file layouts?

@arturenault
Copy link
Author

Actually realized that what I'm asking for already works in a simple repo, but I'm working with a monorepo and it doesn't seem to be supported there.

I have a parent tsconfig.json and child tsconfig.jsons in each monorepo. In each tsconfig.json I've set baseUrl to be src. So relative paths are resolved correctly when I'm in a subdirectory containing a tsconfig.json, but not at the root (which is where I'm currently doing type-checking from). I'm learning that tsc only loads one tsconfig.json at a time, and not the one closest to the file as some other tools do.

I tried setting up projectReferences to get all the projects to get built together, but it looks like that doesn't work with noEmit as described here: #36917

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants