microsoft / microsoft/TypeScript

Spread operator allows invalid return type in record data types

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

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

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

説明

### 🔎 Search Terms

spread operator, return type mismatch on record of functions

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries. Nothing related was mentioned.
I've tried 3.3, 4.0, 5.0 and 5.8.3 and got the same result.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.3#code/C4TwDgpgBAYgdgHgJIBooHkB8UC8UDeUokAXFAEQBmc5A3FNWQBQCWcYArsGUgJS7Z0UAL60AUGOLR4AJQgBjAPYAnACbI0WXFDlK1CAM7BlbAOZp4GjNgA+UDnFURKbCKszixlB-OAtFcESKsgoq6qjWTGJQUACGvv5wBmS6YYbGZmis7Fw8-DiCUHYOTi5wbphivGQheuGa2PjRRAAWyooA7lDlXQCiyu3KTLziwhJKScBENXCp+gBuiiyqaHAcALYARhDK2HhNMbFkhFJkVDRojFDDAlAATCJoAPRPUAgAtFBK6+sQcH5wUxQRRcVosAxQAA2rigyggBg4kOAELYcUCO0GUFRwBa0Gh5Sg20hnWaADpycBgrNQmomIRNsx8thyABmcgiXgoMSiMRAA

### 💻 Code

```ts
type Fn = { type: "fn"; fn: (input: I) => O };

type FnRecord = Record | undefined>;

function toFnRecord(
actions: Record O | undefined>
): FnRecord {
throw new Error();
}

const t: FnRecord = {
a: { type: "fn", fn: () => 2 }, // <- commenting out this line results in an error in the line below
...toFnRecord({ b: () => "3" }),
}
```

### 🙁 Actual behavior

No error was reported

### 🙂 Expected behavior

Expect the following type mismatch error message. This is reported if I **comment out** the entry without spread operator (`a: { type: "fn", fn: () => 2 },`)
```
Type '{ [x: string]: Fn | undefined; }' is not assignable to type 'FnRecord'.
'string' index signatures are incompatible.
Type 'Fn | undefined' is not assignable to type 'Fn | undefined'.
Type 'Fn' is not assignable to type 'Fn'.
Type 'string' is not assignable to type 'number'.(2322)
```

### Additional information about the issue

Similar to #61754, but this example does not involve any explicit generic subtyping constraints (`extends`).

I'm not sure if this is related to #10727.

I'd like to know any workaround that can prevent this type error happening at runtime. I've run into this multiple times with the same pattern used in my codebase and got very surprised each time.

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の再現から始め、spread なしのエントリがある場合とない場合で診断結果を比較します。spread の結果が FnRecord に対してどのようにチェックされるかを調査します。無効な string の戻り値の型が期待される mismatch エラーを生成し、有効な record 値は引き続き受け入れられれば完了です。

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

評価

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

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

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