microsoft / microsoft/TypeScript

Destructuring assignment with computed property name does not help narrow types as intended.

Open
#62,401 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: check: Control Flow Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

"destructuring assignment", "computed property name", "type narrowing"

### 🕗 Version & Regression Information

Happen to all existing version in typescript playground.

### ⏯ Playground Link

https://tsplay.dev/w1eElw

### 💻 Code

```ts
declare let obj: Record

declare let cond: boolean;
declare let key: string;
let result: string | null = null;

if (cond) {
({ [key]: result } = obj);
result;
// ^?
}

if (cond) {
({ 'key': result } = obj);
result;
// ^?
}
```

### 🙁 Actual behavior

Type narrowing did not happen to the assignee variable `result` after the assignment `({ [key]: result } = obj);`.

### 🙂 Expected behavior

Expecting the assignment `({ [key]: result } = obj);` will narrow the assignee variable `result` to non-null.

### Additional information about the issue

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and compare the inferred type of result after the computed-property destructuring assignment and the literal-property assignment. Trace the type-narrowing behavior involved and verify that the completed change preserves the expected non-null narrowing in both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.