microsoft / microsoft/TypeScript

Forward compatible type definitions regarding compiler warnings

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

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

Needs Proposal Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

Search Terms

forward compatibility, experimental features, forward compatible, undeclared types, new browser features, warning undeclared overloads

Suggestion

Currently if a TypeScript .d.ts file contains a function overload with an unknown type it doesn't discard the type but rather treats it like there was an overload that produces any as the return type.

This is more easily demonstrated with an example:

// plus.js
function plus(x, y) {
  return x + y
}
// plus.d.ts
export default function plus(a: number, b: number): number

// Forward compatible definition that should be ignored by older
// versions of TypeScript that don't recognise it or by
// consumers using a "target"/"lib" that isn't "esnext" (or whatever version
// bigint eventually ends up in)
export default function plus(a: bigint, b: bigint): bigint

export default function plus(a: string, b: string): string

Now if we use the function like so:

const value = plus({}, {})

Will produce different results based on if the --lib/--target supports `bigint or not.

In a world that does support BigInt we will get a warning: Argument of type '{}' is not assignable to parameter of type 'string' which means if people are in the future world then it behaves just fine. But if they use it in a non future world then no warning is issued and the type of value is inferred to be any.

NOTE: The fact bigint is used here isn't really important it was just a simple example to write, this applies to any differences between needed target of the d.ts file and target of the consumer.

Use Cases

The main use case is for libraries that wish to be forward compatible with types that might not be configured in --lib/--target by feature detection (or by just working in the case of the example).

Examples

I'm not sure what would be the best way of supporting this, probably another flag like --warnings-ignore-unrecognized-overloads or something along those lines.

Alternatively it could also be syntax powered e.g. the above example could become something like this:

export default function plus(a: number, b: number): number
export default function plus(a: string, b: string): string

supports bigint {
  export default function plus(a: bigint, b: bigint): bigint
}

Some additional non-bigint examples using the supports example syntax to show examples of where automatic upgrading could happen:

export default function sum(iterable: Iterable<number>): number

supports AsyncIterable {
  export default function sum(
    asyncIterable: AsyncIterable<number>,
  ): Promise<number>
}

export default function findElements(node: Element): Array<Element>

supports ShadowRoot {
  export default function findElements(shadowRoot: ShadowRoot): Array<Element>
}
export default function render(canvas: HTMLCanvasElement): void

supports OffscreenCanvas {
  export default function render(canvas: OffscreenCanvas): void
} 

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. new expression-level syntax)

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

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

はじめの一歩

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

調査の方向性

まず、--lib または --target の異なる設定で plus.d.ts の例を再現し、その後、TypeScript が未宣言の型を含むオーバーロードをどのように解決するかを追跡します。警告と条件付き構文という提案されたアプローチを比較し、サポート対象およびサポート対象外の環境向けのテストを実装する前に、bigint、AsyncIterable、ShadowRoot、OffscreenCanvas の動作を定義します。

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

評価

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

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

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