Repository: TypeScript
Issue: Type guard should infer the type of parent object when applied on a property
Labels: Suggestion,In Discussion

# Suggestion

## 🔍 Search Terms

Type guard, parent object, infer, inference

## ✅ Viability Checklist

My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

Typescript should be able to infer the type of an object, if a type guard was checked on a property of the said object. Currently, Typescript does correctly infer the type of the property, but not its parent object.

## 📃 Motivating Example

The following example is very common in data oriented design.

```typescript
interface Type1 { list: string[] }

interface Type2 { list: { [key: string]: string } }

declare const obj: Type1 | Type2;

if(Array.isArray(obj.list)) {
    const list: string[] = obj.list; // WORKS
    const map: { [key: string]: string } = obj.list; // ERROR, as expected
    const objCasted: Type1 = obj; // ERROR, unexpectedly
} else {
    const map: { [key: string]: string } = obj.list; // WORKS
    const list: string[] = obj.list; // ERROR, as expected
    const objCasted: Type2 = obj; // ERROR, unexpectedly
}
```

The following example works and that is good because it is an equally common case in this type of design. 

```typescript
interface Type3 { type: 'type3', data: boolean }

interface Type4 { type: 'type4', data: string }

declare const obj2: Type3 | Type4;

if(obj2.type === 'type3') {
    const objCasted: Type3 = obj2; // WORKS
} else {
    const objCasted: Type4 = obj2; // WORKS
}
```

So I believe the type guards should work the same way. As far as I see, this does not cause any inconsistency in the language or the type system. It is an improvement without any downsides.

## 💻 Use Cases

See the [full example](https://www.typescriptlang.org/play?ts=5.4.0-dev.20240107#code/FASwdgLgpgTgZgQwMZQAQBUCeAHKBGVAb1QBsQBnCALlUpnAHMBtAXVQF9hRJZEUMcUAExFSFaqKYBrKJhp1GLeRHpgGHDlwAmUJCQQw0SAPZhKqYwCMAVjSy4CAHwG4hAbi4g4ACgCCMGARMADoKf0DMbytrYLJKAEpUAF4U1BUAVyhEwmBUPNQTMwgxSmVVZjYkixtY8TdUAHoG1AB1AHkAJQBpAGVc-P68ptQAAQhyAFooAA9cJAgpgOMYQYLTcwBbBGwaYmlZMsVDtQ0q6NrKeuGAUQ6OzoAaVARyVBm56C1V1cLzaIBhF6fOyCAhnGxXZq3e4dJ7pMDvXSfEiYYDsN4kchoHL5NZFVBbHaSGRyWgqI5k8qnaoxOIQSGtTq9Li44ZjSaI+aLGDLH7rYp044VZI0i70xpQu6PZ6vTmfFn5X7FAFAqBaEGuEXRBnQ6XwuVqlForieHjwZBoexQADMoggghoAHJ7bhrY6nloEBAEDRLMZjCQoAgwJpuNBzfwrQAWO0O1DOwRR92oT3eoWhnR6AxGfk0oQam2oZzRjygHzRITBF1oFJVBOux3ZPn4lWUNUF23g6zuCWM7p9dFQTHY5t-GyAtvqlxQGNdnvDdr942m8N8S2CACssdwvv9geDofAq4t04AbNuoOnOMBM-pDHix9ZrQWt8XBKfS15vNXjHAadaq0EZJUkdP0AyDMBGyIUdlXHVUpytLcu2tBkWmWKRXnIcB+CjYIAE40QxLFoNxJUaQnYEzy1GwUN7NCYAw2hsLQXCCM4IA).