Repository: TypeScript
Issue: Outlier case in Excess Property detection in Union (a case that is -more- strict than it should be)
Labels: Bug,Effort: Difficult

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

Excess Property Checks

(1) 

From [Release Notes 3.5](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#improved-excess-property-checks-in-union-types)

> In TypeScript 3.5, the type-checker at least verifies that all the provided properties belong to some union member and have the appropriate type, meaning that the sample above correctly issues an error. Note that partial overlap is still permitted as long as the property types are valid.

<!--
  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.
-->

### 🕗 Version & Regression Information
3.9.7 - 4.3.4 checked in playground to be the same.

### ⏯ Playground Link

[Playground Link](https://www.typescriptlang.org/play?target=7&ts=4.3.4#code/PTAEBUAsFNWgPADtAxgF2gE1AJwK4A2sAZgPY6iI6nI5oCW0AzqKcaHgHb2mehoBPZKAAUAHwCUoei1AAoEKICMUgLZ4maUCl5oAhvT56CBXNACOeejiyVqtBs1bs9Wons2tOsLjz6roVQAjaBwFMBEAJjU9AW1dAyNMTHoGXmM7GlDHFjRIV1AQgl4Ac35SUD1OON9eUADg0Lk5AG85UH4hWABBSNAAXlAWvQAuUE0cQxKAbkKxiamAX2n2zuFugGYBodHxtEnOGbnC0lJ3TlmUMaDT8+XVwXWAFm3h+f2p2aDr2+gqy7GnDwjRw91WbQ6HUU4AAEgBJADKoEREBhAFFQABVAByaIAGgAFNEAYXAaIAIqAAPKY8AAGThaIASqsOjpOJ5vqBemJNq9dgByAUAGmOQtFV34+Ggy1AikAMuSVJhMeglTgBThaBboAhxFAwFAAaywooQ+z0mUQoGI1FU3K2nFIWmMxQA7lhVotway5WAAEJomHdABqcOxAHFuUj8UTSRSfezOWMeZteS9Bm9QOKxSLtGN9ngZbNFIAUciVKrVGrQLBdpFdcHg5stLDYrDyoQ43Dqj2YnrkXqAA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```
// The expected rule for properties of union type (|) is  
// (1) must contain all required properties of at least one union member
// (2) may contain additional properties that belong to any union member

{
  type A2 = {a: string; b: string};
  type A3 = {a: string; b: boolean; c: boolean};
  type A4 = {a: string; b: boolean; c: number};

  {
    // THIS IS THE UNEXPECTED OUTLIER (compiler error happens unexpectedly)
    const b: A2|A3 = {a: '', b: '', c: true}; // ❌ assignment strictly checked, extra prop from A3 not allowed
  }
  {
    // BEHAVING AS EXPECTED (no compiler error)
    const b: A2|A3|A4 = {a: '', b: '', c: true}; // ✔ assignments allow extra props of other union types
  }
}
```

### 🙁 Actual behavior

The line item marked `// THIS IS THE UNEXPECTED OUTLIER` is rejected as invalid for `A2|A3`
even though 
`{a:''",b:""}` contains all properties to satisfy `A2`, and `c:true` should be allowed by `A3`.

The oddity of that is accentuated by noting that the same value can be assigned to `A2|A3|A4`, where `A4` differs only from `A3` in having property-type `c:number` instead of `c:boolean`.


### 🙂 Expected behavior

The unexpected rejection of property `c` should be accepted as it is in the counterexample.


---

### TL;DR :  About the actual status quo limited type checking introduced with ts 3.5

From [Release Notes 3.5](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#improved-excess-property-checks-in-union-types)

> In TypeScript 3.5, the type-checker at least verifies that all the provided properties belong to some union member and have the appropriate type, meaning that the sample above correctly issues an error. Note that partial overlap is still permitted as long as the property types are valid.

That description does not actually match the observed status quo.  For example, that rule would imply that an empty object could always be assigned to a union of object with required properties, and that is obviously false.  So surely what the author meant was 

>    ... the type-checker at least verifies that all the required properties of some union member are provided ...

i.e., the rule for a set of properties to satisfy a union type (|) is  
- (1) must contain all required properties of at least one union member
- (2) may contain additional properties that belong to any union member

This actual observed behavior is stronger excess property checking than the original verbiage.

That would still make the outlier behavior in this bug report a bug.  It's just worth mentioning while we are on the topic.  I have create a separate issue for this #44871.