microsoft / microsoft/TypeScript

Optional properties on function interface breaks inference of type parameters

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

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

Bug Domain: check: Type Inference Help Wanted
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

function interface optional properties generics type parameters inference

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference.

⏯ Playground Link

Playground link with relevant code

💻 Code
interface FuncA<T> {
  (arg: T): void;
};

interface FuncB<T> {
  (arg: T): void;
  x?: string;
};

const outerA = <T,>(func: FuncA<T>, arg: T) => {};
const outerB = <T,>(func: FuncB<T>, arg: T) => {};
const inner = <T extends 'a' | 'b' | 'c'>(cb: (arg: T) => void) => {};

outerA(inner, (arg: 'a' | 'b') => {});
outerB(inner, (arg: 'a' | 'b') => {}); // error
🙁 Actual behavior

In the call to outerA(), the type of the func parameter is inferred as FuncA<(arg: 'a' | 'b') => void>, as determined by the type of the arg parameter.

In the call to outerB(), the type of the func parameter is inferred instead as FuncB<(arg: 'a' | 'b' | 'c') => void>, using the base constraint of inner's T parameter rather than the subtype of that constraint used by the arg parameter.

🙂 Expected behavior

I do not expect the addition of an optional property on FuncB<T> to break the type parameter inference. It seems that inner's T parameter should be inferred as 'a' | 'b' in both cases.

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

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

はじめの一歩

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

調査の方向性

まず、リンク先の TypeScript Playground で FuncA、FuncB、outerA、outerB、inner の例を使って動作を再現します。2 つの呼び出しにおける型パラメーターの推論を調査し、両方で inner の T が 'a' | 'b' と推論され、outerB に示されているエラーが発生しない場合の補完を確認します。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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