Skip to content

Type Guards Don't Work on Parent Generic ObjectΒ #46233

Closed
@GeoMarkou

Description

@GeoMarkou

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions