Closed
Description
Bug Report
π Search Terms
type guard generic parent object
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generic object type guard
β― Playground Link
Playground link with relevant code
π» Code
interface Dog { woof:string };
interface Cat { meow:string };
type Animal = Dog|Cat;
interface House <T> { animal: T; }
type DogHouse = House<Dog>;
function ProcessDogHouse (house:DogHouse) {};
const house:House<Animal> = {
animal: undefined as any
};
if ('woof' in house.animal) {
// We know the type to be House<Dog> but this still errors
ProcessDogHouse(house);
// Weirdly this doesn't error
ProcessDogHouse({
animal: house.animal
});
}
π Actual behavior
Generic types can't be narrowed by checking their child objects.
π Expected behavior
Should be able to narrow a generic object by the types of its children.
Metadata
Metadata
Assignees
Labels
No labels