Repository: TypeScript
Issue: TS2536 thown when using keyof on a property of a generic type as key
Labels: Bug

<!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->
**TypeScript Version:** 3.8.0-dev.20200204

<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
**Search Terms:** 
* TS2536
* generic typing
* keyof

**Code**

```ts
enum Switch {
    A = 'a',
    B = 'b',
}

interface ResultA {
    keyA1: number;
    keyA2: string;
}

type ResultB = number[];
type ResultType = ResultA | ResultB;

interface GenericBase {
    switch: Switch;
    value: ResultType;
}

interface GenericA extends GenericBase {
    switch: Switch.A;
    value: ResultA;
}

interface GenericB extends GenericBase {
    switch: Switch.B;
    value: ResultB;
}

type GenericType = GenericA | GenericB;

function getValue<T extends GenericType, K extends keyof T['value']>(result: T, key: K): T['value'][K] {
    // T = GenericA
    // T['value'] = ResultA
    // keyof ResultA = 'keyA1' | 'keyA2'

    // T = GenericB
    // T['value'] = ResultB
    // keyof ResultB = number | "toString" | [...]

    // unexpected: error TS2536: Type 'K' cannot be used to index type 'ResultType'.
    return result.value[key];
}

const a: GenericA = {switch: Switch.A, value: {keyA1: 1, keyA2: 'A'}};
const b: GenericB = {switch: Switch.B, value: [1, 2, 3]};

// no errors
console.log(getValue(a, 'keyA1'));
console.log(getValue(a, 'keyA2'));
console.log(getValue(b, 0));
console.log(getValue(b, 3));

// error TS2345: Argument of type '0' is not assignable to parameter of type '"keyA1" | "keyA2"'.
// => expected, and the compiler does know what keys can be used
console.log(getValue(a, 0));

// error TS2345: Argument of type '"keyA1"' is not assignable to parameter of type 'number | "toString" [...]
// => also expected
console.log(getValue(b, 'keyA1'));
```

**Expected behavior:**
As long as the correct keys are used, which is covered by TS2345, the index access should not raise an error.

**Actual behavior:**
TS2536 is thrown when compiling, altough the keys should be known due to the generic typing.

**Playground Link:** [typescriptlang.org](http://www.typescriptlang.org/play/?ts=3.8-Beta&ssl=31&ssc=52&pln=31&pc=57#code/KYOwrgtgBAyg7gSwC4GMAWUDeAoKeoCCUAvFAOQCGZANLvgEInkBGN2AvttgiEsAE4AzCimBQASsADOYADZIiOfFADWwAJ4EAjAC4o4CMwEBuOnjWaATHqlJ+PAOanO2JOoAOYyTPmNSBo34AbQBdUzdPCWk5JAAVDzFSbxiiAB8onyR6U25eAWFRKABxUAEEFHoKKTElfClEVDQ9eGR0U2UANwpZMGA9ZPl4z2cuHj4hETESkDKUImAADz4QABMpYtL7Cqqasyh61qbYBvQAOgJ2-C6evoyUkdzxgqnN8sZF5bWNma3K6qw9gdGs0TmhTtk9tdev1or4HhEXj9ykNEt9Zmk0b8coIwCAUEgEAB7EBQBzAJAANW6vQAPLEoB9QF9prMUdQoABpBlLJnrCyEwRQWJBMhQ4BkEIAPgAFPxYUg9LF2RY9ByAJSKkViiVBDkhAHKAD0hqguMWnnxwBWegE-EJ-CFMEsAFYAMwANkVCXIHLIUBQFBAIEJSCgRlN1RWUCQhKgPBWi2j3rIAziCTIpz2cqQYH4JLlmVOYqCFjCHC4KGJtigFD0LK2RFImCB6BBh3O7LFekwFm0ei0yo0BGs5AIZHY7FMlZA1eYddeFSYzdBbca4M71NuQQHUEs7NdIUnXGN+ljtvtUmw06khNkwFOskJDmlZMpm+lFHZZF7WjIarVU5Vre96Ps+r5UjcH5fr2lh-gBV5AXeD5Pi+5IQb00rMOyAAM-6ATOwHIWBaHvlhUCunhx4mueDqxE6roACzOnoBD8A4kCgKGApJpEZDYX6CDrMGoZVFICAOCAFDMHe0axu4FD8BQEDkgIUDcQi5AAEQ-ppUDpNpQ6WJpGbYCexCStyFp8Cs7KBlGSBoGIlYQO4CB3g6KyEtIqjBnAUBwGgFChhY6wBiS4ZgJGCEEUhoGoW+kGflAuHwaZ1H8HatH0UxLFsRxvBqYKGlkAZmhaMZcZCSGNZSGJElSTJMZQPJinKeMhU8WIZABKp+kxjAdiOLpQSnKNIRpSQFndDelnAJaKzRTesUoeBpHQUOv6UUAA)

**Related Issues:**
* possibly, but not quite: https://github.com/microsoft/TypeScript/issues/33521
* more likely, but also not quite: https://github.com/microsoft/TypeScript/issues/21760