Repository: TypeScript
Issue: TypeScript fails to narrow out `undefined` via `typeof` check on generic indexed access
Labels: Bug,Domain: Control Flow

# Bug Report
### 🔎 Search Terms
undefined narrowed 2322

### 🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the [FAQ](https://github.com/Microsoft/TypeScript/wiki/FAQ) for entries about 'undefined' (including via Find on Page)

### ⏯ Playground Link

[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAysCGAnO9gGcoF4oG8CwAUFMVALbxpoBcUAdgK6kBGEiA3ISVGvYgGbwAxhAAqEUpESpeEGg2asOREmAA28WhHRzGLdoQC+hUJCgAFdZuAp0WXJxLlKOhfuXFSAe0+1qdXYoOxJ6ITACWCKpmrGGeACYuekpcIeGRAJK0gqphtKixtImBBEYEucCsAsLmllpIIABC8WEQaACy8GD27lAA5BYaWr1QNANWNmjJJL0oiMM0sxNKpYI+aMBQAOZaNgBiiJ6kMFp2fPRZwAUAPEFQIgByUBAAHhW0cRgA1hAgnnw1gwQiEazVaHTAABpCAA+AAUt3WqGOG1GSEu8FUVzGdWBTTiLXanQA2g8ALrQqE9RHAe7wUjQGjfX7-EwQP7cBDWLSUgCUI38rm6XFWvg2YE8b3RqgASq16KoNthqciidTafTSVNiAB6bUAQTi+NomygiAgcXOcQ0GwABgAyO1QcWSsIY2U8BVQACEmGwlogfFy5ptUGAnlDAAtoCL8ZcfBjKVxdcAo09EAdEGRWmh4NsoIIIxpthgWMAAO4QCC0KAANwALAA6ADsDbrUA0cVrjYAHA2AAwAWlL8FuYT4sNZ7OdVal7vlGx92F6-sDmjivT5+B6XGntFncoVWq4UF1AAlPDXWO2mBfoGHuJXUYgpVjakCQfiwcSydCiUz2di754gS4IkvcpKkrcSbagAoumIShmgsIAEwAMzIchfIsKonhljQdzgNA-RvvUwFfmAYGkn+PwASRuKgoSFFkqSUAAD5QCuQbrlAYQYLQErtpQYSbHkTCqHe4asn08h6L0DZQSQuoAOpRmaUBxJ4rSRqgTyvFIUA2uxnFriGqz0lAfAHKQ7Z8BUmaFjWuQmnk8EVp2KZ3oRUDsh5BkiusNoAPwKcQZrALw1a7vuHrAEeBhPKoaDQFux4kGFEVQH2cWGIQQA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
type StarStats = {
    mass: number;
    surfaceTemperature: number;
    planets: number;
}
type PlanetStats = {
    mass: number;
    moons: number;
    orbitalPeriod: number;
    orbitalInclination: number;
}
interface PlanetaryBodiesMap {
    'Planet' : PlanetStats;
    'Star' : StarStats;
}
const getStatFromSet = function<
    TN extends keyof PlanetaryBodiesMap,
>(
    statSet : Partial<PlanetaryBodiesMap[TN]>,
    statName : keyof typeof statSet,
) : number {
    const potentialResult = statSet[statName];
    //Adding redundant `&& potentialResult !== undefined` to the conditional,
    //the error message changes between v4.7.4 and v4.8.0-beta
    if(typeof potentialResult !== 'undefined') {
        potentialResult;
        //Hover above to see: Partial<PlanetaryBodiesMap[TN]>[keyof PlanetaryBodiesMap[TN]]
        //Error ts(2322) below:  Type 'PlanetaryBodiesMap[TN][keyof PlanetaryBodiesMap[TN]] | undefined' is not assignable to type 'number'.
        //Where does that extra `| undefined` come from after having narrowed the type of the `const`?
        return potentialResult;
    } else {
        return 0;
    }
}
```

### 🙁 Actual behavior
TypeScript complains that a constant value can be `undefined` within a conditional block where that is already checked for and excluded.

### 🙂 Expected behavior

1. Inside a conditional which can only be entered if a constant type is not `undefined`, TypeScript excludes ` | undefined` from the possible types of the constant. 
2. When showing the type of a constant as X in an error message about how that can't be assigned to Y, the type X should be the same as the type shown when hovering over the type.  In this example, they differ by `| undefined`, which matters.  In the unsimplified version where the type is more complex, the type of the constant shown on hover is very linguistically different from the type shown in the error, making this issue harder to debug.