Repository: TypeScript
Issue: Type guard cannot remove `undefined` from `Partial<T>[keyof T] | undefined`
Labels: Suggestion

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->
When writing a function to merge object `A` with non-undefined values from object `B` into `C` where `A` is of type `T` and B is of type `Partial<T>`, I came across a situation where the type guard seems to be failing.


### 🔎 Search Terms

<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->
- type guard
- Partial<T>

### 🕗 Version & Regression Information

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _common-bugs-that-arent-bugs_

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?jsx=4&ts=4.4.0-beta#code/GYVwdgxgLglg9mABAWwKYCcDmqDSqCeAzgDwAqAfABQCGAXIqQDSIBG9ACterNQDZnlmAawKF6lEfjjAGASgDaAXVn1SiAN4AoRDsQQEhKIgBuqxAF4NiAHS3qiAL4BubbuBx0iSvrCHEkxGl-UVkNV11dHz9jPhBUC1Z5SUUXCIiYGUoY3jjEAEJzS3AAE1RgGDBUYtCtNLqTJIJFBOy41PqHcMcu9FQoEHQkYxdOzVBIWAQUDGw8IgBJQgARMoqqgRpVZjZETm4YPgFhUXFJINIFZTNayIMjUwYE9Rs7R3add09vO+D8QJlJIQal0IlF7rF4pYWI18CkQboMl4YMtVpVilkIbJgfU0sYYc1LK1UO80p0ImTdL1+oMTCNNJoKlAMMBqBB4gAhMIRagAfnoLDgcF4qGoYDpDLATPQLLZiAAgohUAAPJlgYqERCcm46OisQXC0Xi8bQeBINBYVBy9mbeXbejs45EU4EIJyy7Y26+e70BWWZ62az2ZxdT5eMG-f6-IFc+rhokJaHJEnpTLxgpFNWoqoenE6PHJFoQ5O6Ck6UuIKkDIZG8Amqbm7ByvY8Xg2uV23ZcFvEOWCKPOqQyN1KHN6H4PX1WANBkmh75eiNBQGj0HjiEJ-HFnSIjE5eLpxAlLPVGO5hoFwlF+Flrrlys04aaUbGyZIZErcpoja6tQAH0Pmaftm9D2MijzahWfRVog9gHkeQHFHSQA)


### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
function merge<T>(a: T, b: Partial<T>, keys: (keyof T)[]): T {
    const v: T = { ...a };
    for (const key of keys) {
        const value = b[key];
        if (value !== undefined) {
            v[key] = value;
        }
    }
    return v;
}
```

### 🙁 Actual behavior

<!-- What happened, and why it was wrong -->

There is an error at `v[key] = value;`:
```
Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
  Type 'undefined' is not assignable to type 'T[keyof T]'.
```

![image](https://user-images.githubusercontent.com/3740137/127733201-d6ced402-0bab-471b-89a9-31ab566d4dd8.png)

Clearly `value` is NOT `undefined`, yet the compiler complains that it is.

### 🙂 Expected behavior

<!-- What you expected to happen instead, and why -->
I would expect that the code compiled.