microsoft / microsoft/TypeScript

Allow to specify import sorting.

Open
#63,780 6 comments 10 reactions 0 assignees View on GitHub
Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Typescript has this long running issue full of complaints about how there's no way to prefer certain import paths over others, and in any non-trivially sized application, path aliasing is bound to clash with the various library imports available (hence the practice of having a path alias `@/` to get at all first-party application imports).

https://github.com/microsoft/TypeScript/issues/51155

A plugin was suggested multiple times in the above issue, which has a very simple interface to configure around this: https://github.com/tidalhq/ts-plugin-sort-import-suggestions

I think this is something that should be included in tsgo itself, for two reasons:

- It's not just some fringe preference, everyone at some point will want to prefer certain import path prefixes over others
- It's an extremely simple thing to implement

I propose being able to add to tsconfig.json a configuration item like this:

```
{
"compilerOptions": {
"importSorting": {
// Matches `@/`, `../` and `./`, move them up in the suggestions (This is the default config if you leave it empty)
"moveUpPatterns": ["@/", "\\.{1,2}/"],
// Move `dist` down in the suggestions, by default it's `[]`
"moveDownPatterns": ["dist"],
]
},
}
```

This is a flexible and performant approach that will handle pretty much everyone's needs. I don't know why this was left to be a plugin when it's such a critical piece of an ergonomic Typescript-based development environment.

Contributor guide

Open the contributing guide

Research direction

Start with the linked TypeScript issue and the ts-plugin-sort-import-suggestions interface, then locate tsgo's import-suggestion handling and tsconfig.json option parsing. Done means a compiler option accepts moveUpPatterns and moveDownPatterns and import suggestions reflect those patterns, with coverage for the proposed examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, typescript
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.