microsoft / microsoft/TypeScript

Error from `exactOptionalPropertyTypes` not reported when using object spread with a ternary and optional property acces

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

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

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

説明

### 🔎 Search Terms

exactOptionalPropertyTypes
object spread
ternary

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about exactOptionalPropertyTypes, object spread.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true&ts=5.9.3&ssl=5&ssc=32&pln=1&pc=1#code/C4TwDgpgBAYg9nKBeKBvAUFKYCGAnCAO2AEkATAfgC4oBnYPAS0IHN0BfddMiAYwBt80XnEL0oBAI4BXCPQBCcMiBrw4Abi78IwKMHwsdqhJvQB6M3QAWcAO60oEAB6RewCGUd48cPFDKyeogAok44bgDyYMCMojj8AAo+kHigACrgcuj6eIa6KKjYQsTkNFKyCkogAHS4BCWe7KYWUAC2jLS0zCxePn62VkRQ0l2sdGAEOJ44hJ7ueIT4INkGOshoUNVbABQMgRQbdUSkZGUQMnLAisq1xSdQ7FA0qOwAlA-qUC3yAKoA4lwRGJdIUjg0HutypdriBNDk8utCltqrs8PtDndSkV6vdHs83h8sF9LL8AeZLBBvL4oDgwJB8A4RN4+MB+CAoIwAGZQABu8UCHSghDguhw2GSlNANN4vDktA8UE5PlaNMIUDgACMAFYs9BA8Rgk4AJnWAHJTTSHPQmGMAD7DWYQTnMDxw1b5DbI1Ho0GY07Y47kE14tAEproIA

### 💻 Code

```ts
type Foo = {
parentId?: string
}

declare const requestBody: Foo;

let target: Foo;

// shows expected error due to ExactOptionalPropertyTypes
target = { parentId: requestBody.parentId };

// missing error when using spread and ternary
target = { ...(true ? { parentId: requestBody.parentId } : {}) }; // BUG

const { parentId } = requestBody;
target = { ...(true ? { parentId: parentId } : {}) }; // BUG

// error appears correctly if value is not a property accessed from an object
const parentId2 = '' as string | undefined;
target = { ...(true ? { parentId: parentId2 } : {}) };

```

### 🙁 Actual behavior

The expected error is silenced only in the specific case that combines object spread, ternary, and accessing an optional property from an object, although the types are always recognized correctly with `| undefined`.

### 🙂 Expected behavior

All scenarios demonstrated in the code snippet are expected to show the same error.

### Additional information about the issue

_No response_

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground から始め、exactOptionalPropertyTypes の再現を実行します。直接代入、オブジェクトスプレッドの三項演算子、分割代入されたプロパティ、ローカル変数のケースを比較します。spread と ternary のケースが他のシナリオと同じ期待されるエラーを報告したら、issue は完了です。

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

評価

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

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

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