Suggestion
π Search Terms
Type guard, parent object, infer, inference
β
Viability Checklist
My suggestion meets these guidelines:
β 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.
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.
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.
Suggestion
π Search Terms
Type guard, parent object, infer, inference
β Viability Checklist
My suggestion meets these guidelines:
β 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.
The following example works and that is good because it is an equally common case in this type of design.
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.