Repository: TypeScript
Issue: ThisType would disturb  Generics' inferring type when assign an expression to a type variable
Labels: Needs Investigation

**TypeScript Version:**  3.9.2, and it does exist in 'Nightly' version, too.

**Search Terms:** ThisType, Generics, Inferring problem

**Code**

```ts
type customFunc = (...args: any) => any

declare function SimpleVue<
  D,
  C extends { [K in string]: customFunc },
  M = { [K in string]: customFunc },
  CC = { [K in keyof C]: ReturnType<C[K]> },
  ReturnedD = D extends customFunc ? ReturnType<D> : never,
>(option: {
  data: D,
  methods: M,
  computed: C
} & ThisType<M & ReturnedD & CC>): ReturnedD

const instance = SimpleVue({
  data() {
    return {
      firstname: 'Type',
      lastname: 'Challenges',
      amount: 10,
    }
  },
  computed: {
    fullname() {
      return this.firstname + ' ' + this.lastname
    }
  },
  methods: {
    hi() {
      alert(this.fullname.toLowerCase())
    }
  }
});
```

**Expected behavior:** 

We expect `ReturnedD = D extends customFunc ? ReturnType<D> : never` infers `ReturnedD` to `ReturnType<D>` at runtime since `D` fits the shape of `customFunc`:

```
() => {
    firstname: string;
    lastname: string;
    amount: number;
}
```

And the code should show no error in TypeScript environment.

**Actual behavior:**

The actual behavior is that `ReturnedD` is inferred to `never` type since `D extends customFunc` here is false at runtime.

You can also check it with the returned type of `instance` as well.

**Playground Link:** 

Check out the link [here](https://www.staging-typescript.org/play?ts=3.9.2#code/C4TwDgpgBAxgrgZ2AewLYDE4DsZQLxQAUAdKQIYBOA5ggFxRlYgCU+AfA0wFBcAmEMADaVoAM2wxgAS2RYoAZSmowgiADU4EADxcoUACIAaXVADCUCAA9gELLwRQA3lADaAaShS5SCl6oBdengkNEwcKABfYz0AWXwnVw8vKB8-QNhEFAwJSOizcwJnd085AGsIEGRRM3SAJQhgOAosABVwbVN3fw4ok3rG5ohefXiRqxs7B2CssNwAfih+ptb2rX0OeiwIADcICmM2QmQwaVl6RxNeMmAyeiMTVAaAC2R7ehi8mDQwOBteelMXAiUAAZFAWk8pAg2pAtHEwUtBsNQfk2Mx6IitsMeF8sEgSkhGDBoARFMpVBoIIQLnorjdCKwaXooBQGssnCZmVBRFIKEgsGRHvQAOQwiDCvJc4T8wUQEWmJ5kQSqLBUCAICWc5mC5DYYD0ACMAAZJZETL09F9lL8hucteJlQLHgyOVy9KyBnJgJCEMQeXzgE7oABqKDCsNQUPeqHEaWB2VaiLmvKPb2vOiu5mQl1MrlKvbAQjR30OwRB4goAAyyAA7ntTGQEFTmMxE+agcwANxcIA).

**Personal speculation:**

If you look at the demo above, you can do following steps to remove the error hint and confirm `ReturnedD`'s correction:

1. Remove `ReturnedD` from `ThisType<M & ReturnedD & CC>`;
2. Save the file;
3. Error disappeared on `this.firstname`, etc;
4. The returned type of `instance` inferred from TypeScript is as expected and would not be `never`;

I also reproduced it with TypeScript [playground](https://www.staging-typescript.org/play?ts=3.9.2#code/C4TwDgpgBAxgrgZ2AewLYDE4DsZQLxQAUAdKQIYBOA5ggFxRlYgCU+AfA0wFBcAmEMADaVoAM2wxgAS2RYoAZSmowgiADU4EADxcoUACIAaXVADCUCAA9gELLwRQA3lADaAaShS5SCl6oBdengkNEwcKABfYz0AWXwnVw8vKB8-QNhEFAwJSOizcwJnd085AGsIEGRRM3SAJQhgOAosABVwbVN3fw4ok3rG5ohefXiRqxs7B2CssNwAfih+ptb2rX0OeiwIADcICmM2QmQwaVl6RxNeMmAyeiMTVAaAC2R7ehi8mDQwOBteelMXAiUAAZFAWk8pAg2pAtHEwaZTGxmPQloNhjwvlgkCUkIwYNACIplKoNBBCBc9FcboRWJS9FAKA1lk4TAyoKIpBQkFgyI96AByGEQAV5dnCHl8iCC0xPMiCVRYKgQBCitkMvnIbDAegARgADGLIiZenovspfkNzurxAreY9aaz2XomQM5MBIQhiJzucB7dAANRQAXBqBBj1Q4gSv1S9URE15R4e150J0MyGO+ns+V7YCECNe22Cf3EFAAGWQAHc9qYyAhycxmHGTUDmABuLhAA).

We can confirm that:

1. `ReturnedD` runs well in inferring type at runtime;
2. The wrong type may be caused by some mechanism inside `ThisType`;