Skip to content

Automatically resolve custom path aliases in build #32479

@lffg

Description

@lffg

Search Terms

automatically resolve custom path aliases in build generated code

Suggestion

I personally can't find the reason why TypeScript does not automatically resolve custom path aliases in the build.

TypeScript is a compiler, so I think that it should take care of that.

To avoid any breaking changes, I think that introducing a new option such as resolveCustomPaths is a good solution.

Use Cases

When I have the paths option set in the tsconfig.json, TypeScript will automatically resolve the import paths in the generated code.

Examples

If I have the following tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@core/*": ["app/core/*"]
      "@services/*": ["app/services/*"]
    }
  }
}

And I have the following /src/app/http/controllers/UserController.ts file:

import { ... } from '@services/my-service.ts';

Would be nice if the UserController.ts could be compiled into something like:

// UserController.js (Compiled)

// BaseURL (tsconfig.json `src`) + CustomPath (tsconfig.json `app/services`) + RequiredFile (`my-service.ts`)
const { ... } = require('src/app/services/my-service.js');

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions