microsoft / microsoft/TypeScript

Destructuring object with generic key should reject undefined

Open
#60,119 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

🔎 Search Terms

destructure, generic key, undefined

🕗 Version & Regression Information

This is the behavior in every version I tried (back to at least 5.0), and I reviewed the FAQ for entries about destructuring

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20241002&ssl=7&ssc=1&pln=7&pc=2#code/JYOwLgpgTgZghgYwgAgCrIN7LgfgFzICuIA1iAPYDuIA3MgEb5GkXV0JPFlUjIC+AKAExiCMMHK8YAHgAyyCAA9IIACYBnZCQgBPcjDQA+ABQkCsgDTIwBdAB9mqiDFARVASgJdWvDAOTICJLqYJjIANokALoEAG78yAC81sgA9KnI6gAW5IQANqoKUFDkUMjGCFBw2RCawAahxE4uIG7u-oHBoQD6sQBMSdaRUWkZEACOhMCxcHkQ4J1OVqrktUUlUB1QEGCEULyxAnxAA

💻 Code
interface T { a?: unknown; b?: unknown; c?: unknown }

function f<L extends keyof T>(k: L, t: T | undefined): unknown {
  const { [k]: v } = t // should error (crashes if t undefined)
  const _v2 = t[k] // equivalent code, does error
  return v
}
🙁 Actual behavior

TS does not error on the destructuring, even though it will crash at runtime if t is undefined, and it errors on equivalent code using object-access syntax.

🙂 Expected behavior

TS should error on the indicated line.

Additional information about the issue

This is simplified from an actual example in our codebase. Obviously in this case it's silly to destructure but this was part of a larger block of destructuring so the code would at a glance seem reasonable.

Note that the destructured value does end up having a funky type (T | undefined)[L], which presumably it's hard to do anything with. But of course it's still assignable to unknown, and thus usable in various ways. (In the actual code in question, the authors were doing other complicated things, running an issue similar to #40312, and ended up casting through unknown to the type they wanted.)

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 Playground example and compare the checker’s handling of computed-property destructuring with the equivalent indexed access expression. Trace the generic key and possibly undefined object through type checking; done means the destructuring line receives an appropriate error without regressing valid destructuring.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.