microsoft / microsoft/TypeScript

Suggest imports to self-reference a package by its name when it has an `exports` field

オープン
#54,080 コメント 1 件 リアクション 21 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Domain: LS: Auto-import Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Suggestion

🔍 Search Terms

Package self reference auto import

✅ 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

Node.js supports self-referencing a package using its name when it has an exports field. Note this is distinct from self-referencing a package working in some monorepo setups where there are symlinks from node_modules to the packages. This doesn't rely on any symlinks, it looks up to the nearest package.json and if it has a name and exports field, importing that package name resolves there. This works correctly with moduleResolution nodenext/bundler but TypeScript doesn't suggest auto-imports to the package name or have completions to the package name in import specifiers, it would be nice if it did.

This is essentially the same request as #52460 but about a package's own exports rather than its imports.

This is also sort of a request for the opposite behaviour than requested in #26044 except that this is specifically only about packages that have an exports field where TypeScript is configured to use the exports field. I don't think this really conflicts with that issue since the problem there is that without exports, it can lead to imports like my-pkg/src/something which you don't want but since package self-referencing like this requires an exports field, imports like that wouldn't be allowed.

📃 Motivating Example

Let's say you have a package that looks like this:

// package.json
{
  "name": "my-package",
  "type": "module",
  "exports": "./index.js"
}
// index.js
export function doSomething() {}
// index.d.ts
export declare function doSomething(): void;

And you'd like to write tests that reference the package name so it's easy to see that the tests only use the package's public API specified in exports, you can already do this and it will correctly compile with moduleResolution bundler/nodenext but with this feature, the suggested import would be to my-package rather than ./index.js.

// index.test.ts
import { doSomething } from 'my-package';

// some tests...

💻 Use Cases

As mentioned above, being able to easily write tests that only use a package's public API in the same way that users would use it via the package name without having to work around auto-import by changing the import after auto-import suggests a relative path.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

moduleResolution nodenext または bundler の下で、name フィールドと exports フィールドを持つ package.json、index.d.ts、index.test.ts を使用して例を再現します。まず、auto-import と import-specifier の補完におけるパッケージ名の処理を追跡します。完了の条件は、既存の相対パスの候補を壊すことなく、TypeScript が公開エクスポートのパッケージ名を提案することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
node.js, typescript
領域
developer-experience, tooling
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。