microsoft / microsoft/TypeScript

Node module dependencies that use a triple-slash directive to reference libs poison the global scope

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

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

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

説明

TypeScript Version: 2.x, 3.x

Search Terms: lib.dom.d.ts pollutes global scope, name is defined as never, event is defined as Event | undefined

See: https://github.com/AaronFriel/is-test/blob/4e235decffdfd3c137ea066f8462d57d2fc8d0f2/src/index.ts at the commit hash linked.

Expected behavior:

Given a tsconfig.json with lib: [], I would expect the following code to fail to build due to $variable not defined errors:

import is from '@sindresorhus/is';

function main() {
  console.log(name);
  console.log(event);
  console.log(caches);
  console.log(applicationCache);
  console.log(parent);
}

Actual behavior:

See: https://github.com/AaronFriel/is-test/blob/master/src/index.ts for a particularly egregious example of this. It builds without any type errors. When running npm run start, it will throw a runtime error on the first console.log. If you remove that line, the next one fails, and so on, for the next 700+ lines.

General purpose libraries often need to be written to handle multiple versions of ES (ES5, ES6, ES7...) and multiple host environments (e.g.: DOM, ScriptHost, WebWorker). But right now they are faced with a choice:

A: Use a triple slash directive to import types that they use, and poison the user's global scope, causing nearly one thousand variable names to be silently accepted as defined without any type checker errors.

B: Remove the triple slash directives and require that users use the --skipLibCheck option or equivalent in their config file or include the required libs in their compiler options. Otherwise the library will fail to type check.

This is a choice that library authors would quite reasonably not want to make.

Potential solutions:

  1. Triple slash directives should import types only in dependencies.

    This seems tricky to implement, I think.

  2. There should be a types-only version of the major lib files, so that general-purpose utility packages can reference those instead.

    This seems less tricky: extract all the types from the dom lib a new dom-types lib, and use a directive to reference the types. Then, library authors can triple-slash reference dom-types and get only the types. Has the advantage of not causing any compatibility issues, and library authors can opt in to the types-only package.

  3. ???

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

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

はじめの一歩

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

調査の方向性

リンクされた src/index.ts の例と lib: [] を使用する tsconfig.json で、まず挙動を再現する。triple-slash ディレクティブと lib.dom.d.ts が依存関係の型チェックにどのような影響を与えるかを調べる。完了条件は、ライブラリの作者に参照している型の削除を要求することなく、例に含まれる宣言されていないグローバルが型エラーを生成することであり、特定のテストファイルは指定されていない。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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