Repository: TypeScript
Issue: Unexpected effect on type of RHS expression based on use in assignment expression
Labels: Needs Investigation

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

(All search terms are in title)

<!--
  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

<!-- 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 changed between versions 4.2.3 and 4.3.5

It still repos with 4.7.4, 4.8-beta and the current nightly (4.8.0-dev.20220712).

### ⏯ 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?ts=4.7.4#code/C4TwDgpgBAKu0F4oGdgCcCWA7A5lAPlFgK4C2ARhGgFDUA2EwUA9uQFYBcUAShAMbM0AEwA8qTLgA0seAD4oSAN4BfagDNiWPsAzMsUYBFQiYUCAA9DWIchmRZACgBuAQzpcYASg9RF1KFAYalAOAAyevv4BUAD0MbAAFhi2oJB8CfwA1rbkxEzJUADugkIYVAYJLvpYEBBCdQB0UQGsbADaAOSGqB0AugpQrnQA3M1QaIzEaPpDowGqUXFQAHIYfNBVQlACWMjJENK5TGouGHQp8NsZfJnYOE0BE8BT+q2d3cB9A7PUCwxM4juAw+Dg6GTodGYHQiS2QCWYxDoW0oUDU2AgQA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
type Type = string | number

let obj: Record<string, Type> = {}
function test<T extends Type>(val: T): T {
  if (0) {
    // This typechecks but is wordier than needed.
    obj['test'] = val;
    return val;
  }

  // Nice and concise, but fails type checking.
  return obj['test'] = val;
}

let string = test('hello') // should be fine
```

### 🙁 Actual behavior

Fails typechecking. It appears to be because the expression `obj['test'] = val` is given the type of the broader LHS (`Type`) rather than the narrower type of the RHS (`T`). 

### 🙂 Expected behavior

This should typecheck correctly. Given that the RHS of an assignment always needs to be more narrow than the LHS, it seems like it makes the most sense to use the RHS. One possible exception would be to fall back to the LHS if the RHS is `any`.

This would also be nice for consistency with top-level code where it [seems to work correctly](https://www.typescriptlang.org/play?ts=4.7.4#code/C4TwDgpgBAKu0F4oGdgCcCWA7A5lAPlFgK4C2ARhGgFDUA2EwUA9uQFYBcUAShAMbM0AEwA8qTLgA0seAD4oSAN4BfWgyYAPBS3YBtAOTAIqfQF1t+gBYZ9AbmoB6B1CgA9APy0BWVFBDbWNgMjE3MkKxt7JxcXD1pqIA)