Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GeoMarkou opened this issue Oct 5, 2021 · 3 comments
Closed

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

GeoMarkou opened this issue Oct 5, 2021 · 3 comments

Comments

@GeoMarkou
Copy link

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.

@MartinJohns
Copy link
Contributor

Duplicate. See this comment: #31755 (comment)

@MartinJohns
Copy link
Contributor

Here's a recent duplicate that's still open: #42384

@GeoMarkou
Copy link
Author

Thank you for finding the duplicate, I'll close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants