microsoft / microsoft/TypeScript

"paths" option should allow untyped entries

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

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

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

説明

Search Terms

paths option untyped declare module

Suggestion

The existing "paths" option allows you to control the types used for bare specifiers in your source code, by mapping them as keys to a list of candidate target specifiers.

// tsconfig.json
{
  "paths": {
    // <bare-specifier>: [ <candidate1>, <candidate2> ]
    "foo": [ "external-foo", "fallback-foo" ]
        // if "external-foo" is not found, "fallback-foo" will be attempted, otherwise error
  }
}

I suggest we allow the final element in the array of candidates to be the empty string "". If hit, it would mean "This module exists but is untyped."

Use Cases

Our build system uses "paths" to implement a searching loader for typed dependencies. We know all the acceptable source specifiers ahead-of-time, and so create one property for each - this ensures precise auto-completions for external dependencies in VS Code. We do not know if the target specifiers will exist on disk or not, e.g. the user may not yet have populated the disk via "npm install".

Strict: Some users want to be strict and see errors if no match was found. This behavior comes for free from "paths" today.

Sloppy: Sometimes users want to be sloppy and have the source specifier be typed as any if no match was found. This behavior is not possible today because there is no way to express "this modules exists but is untyped" in "paths" today.

Workaround

We have not discovered any alternative that allows sloppy behavior to be implemented.

The closest workaround is to write declare module "foo" in an ambient declaration file. However this does not work because it clobbers (takes precedence over) the same entry in "paths". Our desire is for untyped behavior to be the last resort, not the first choice.

Examples

Current behaviour: This works today and is fine for implementing strict behavior.

// tsconfig.json
{
  "paths": {
    "foo": [ "external-foo", "fallback-foo" ]
        // assume "external-foo" and "fallback-foo" do not exist on disk
  }
}

// index.ts
import aDefaultImport, { aNamedImport } from "foo";  // Error(2307): Cannot find module

Proposed behaviour: Here's how we could achieve sloppy behavior.

// tsconfig.json
{
  "paths": {
    "foo": [ "external-foo", "fallback-foo", "" ]
        // assume "external-foo" and "fallback-foo" do not exist on disk
  }
}

// index.ts
import aDefaultImport, { aNamedImport } from "foo";  // no error

The usage of "foo" would result in no compile-time error.

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.

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

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

はじめの一歩

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

調査の方向性

ファイル、テスト、またはエントリポイントは指定されていません。まず、コンパイラによる tsconfig.json パスの処理とモジュール解決を見つけ、次に、最後の空の候補が型付けされていないモジュールを表し得る仕組みを追跡します。提案された設定で、先行するパス候補が存在しない場合にエラーなくインポートをコンパイルできれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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