lib.d.ts types are too loose: provide a version without vars declared?

オープン
#327 コメント 2 件 リアクション 4 件 担当者 0 名 GitHub で見る

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

評価

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

調査の方向性

ファイル、テスト、エントリポイントは指定されていません。まず、このジェネレーターがどのように lib.d.ts の宣言を生成するかを追跡し、次に既存のグローバル宣言を維持したまま、型のみのバリアントをどのように生成できるかを判断してください。ジェネレーターが要求された分離を公開し、その生成された出力がここで説明されているブラウザー対型のみのユースケースを検証できれば完了です。

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

説明

(Porting https://github.com/Microsoft/TypeScript/issues/20175)

A lot of teams have seen production bugs from trying to use code not available in older browsers. performance.now, for example, is listed in lib.d.ts but shouldn't be used on sites that support IE10 and don't polyfill performance.

Two other ways of getting around this class of problem without dropping browsers or adding polyfills (are there other good ones?) are:

  • Manually banning scores of built-in types with TSLint
  • Dropping "dom" from "lib" in tsconfig.json

Removing "dom" typings works great to enforce using only non-browser APIs (or dependency-injecting them where used), which is much better than calling APIs like performance.now and setTimeout willy nilly. However, you now need to manually re-write typings for all these native things as you go.

The solution would be much simpler if TypeScript exposed a form of lib.d.ts that included only the interfaces and types for all these constructs without the declare var/declare function.

Using setTimeout as an example, it's declared without a separate ISetTimeout interface in TypeScript's lib.d.ts:

declare function setTimeout(handler: (...args: any[]) => void, timeout: number): number;
declare function setTimeout(handler: any, timeout?: any, ...args: any[]): number;

interface WindowTimers extends Object, WindowTimersExtension {
    // ...
    setTimeout(handler: (...args: any[]) => void, timeout: number): number;
    setTimeout(handler: any, timeout?: any, ...args: any[]): number;
}

If it were changed to provide an ISetTimeout it would be much easier to use setTimeout without a reliance on the browser+Node API.

interface SetTimeout {
    (handler: (...args: any[]) => void, timeout: number): number;
    (handler: any, timeout?: any, ...args: any[]): number;
}

interface WindowTimers extends Object, WindowTimersExtension {
    // ...
    setTimeout: SetTimeout;
}
// Only in the standard ("loose") version:
declare var setTimeout: SetTimeout;

Errata: I personally remove "dom" whenever possible.. but a lot of @types definitions assume it exists and it's a bit like whack-a-mole trying to fix typings on DefinitelyTyped to work in dom-less repos.

Edit: some DefinitelyTyped issues this causes:

主要言語
TypeScript
スター
740
フォーク
474
平均マージ
2日 8時間
マージ済み PR(30日)
15

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

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

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

microsoft/TypeScript-DOM-lib-generator のほかの issue

microsoft/TypeScript-DOM-lib-generator の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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