microsoft / microsoft/TypeScript
Can't destruct a Partial<T> into T that uses default values
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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