microsoft / microsoft/TypeScript

Can't destruct a Partial<T> into T that uses default values

Open
#42,053 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version: 4.2.0-dev.20201219

Search Terms: Destructuring with partial and default value

Code

type T = {a: number};
const t: Partial<T> = {};
const {a = 10}: T = t;

Expected behavior:

This should be allowed. For each prop with a default value, the undefined type should be Exclude'd from the source object.

Actual behavior:

Throws:

Type 'Partial<T>' is not assignable to type 'T'. Types of property 'a' are incompatible. Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.

Playground Link:
https://www.typescriptlang.org/play?ts=4.2.0-dev.20201219#code/C4TwDgpgBAKlC8UDeBDAXFAdgVwLYCMIAnAXwG4BYAKAGMB7TAZ2CmAwAUUjgBLFAGwA8MAHwJk5avSYtU4gIwAGEhjiJgZIA
Related Issues:

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 provided TypeScript Playground reproduction and compare the destructuring assignment's expected and actual behavior. The issue names no source files or tests, so trace the type-checking path for destructuring with a default value and Partial. Done means the example is accepted while preserving the intended type safety, with coverage for this case.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.