Microsoft/TypeScript

Spread operator mixed with destructuring assignment resulting in unsoundness

Open

#54,402 创建于 2023年5月25日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
Domain: flag: noUncheckedIndexedAccessHelp WantedPossible Improvement

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Bug Report

This might have been covered before but I am not sure how to best search for it, apologies if it is a duplicate - when destructuring assignment is used with a spread operator (see example below) TS seems to ignore the possibility that the variable could be assigned undefined.

🔎 Search Terms

spread, destructure, destructuring

🕗 Version & Regression Information

  • This is the behavior in every version I tried (since 4.1.5 which is the oldest version on the playground with noUncheckedIndexedAccess available)

⏯ Playground Link

Playground link with relevant code

💻 Code

const [a] = [...(false ? (["a"] as const) : [])];
//     ^?
const b = false ? (["a"] as const) : [];
//    ^?
const c = [...b];
//    ^?
const [d] = c;
//     ^?

const [a_prime] = [...(false ? (["a"] as const) : [] as const)];
//     ^?

🙁 Actual behavior

a is typed as "a", while d and a_prime are typed as "a" | undefined". Note this only happens when noUncheckedIndexedAccess is enabled, otherwise all of them are typed as "a".

Note that d is the unrolled version of a which makes it more surprising that TS is typing them differently.

🙂 Expected behavior

They should all be typed "a" | undefined.

贡献者指南