Skip to content

Generic type with type variable is narrowed for arguments in a function call, but not in conditionals inside the function body #52327

Closed
@karex

Description

@karex

Bug Report

🔎 Search Terms

generics narrowing
generic type narrowing
dependent type narrowing
type narrowing control flow
type narrowing conditional

#50652 is probably related, but this current report 1) operates with generics and 2) shows that that the narrowing of function call arguments actually works opposed the the conditional inside the function body, which imho permits this being a new issue.

🕗 Version & Regression Information

This is the behavior in every version, including the current nightly, and I reviewed the FAQ for entries about Generics, Type Guards, etc.

⏯ Playground Link

Playground link with relevant code

💻 Code

type Genus = "canis" | "felis";

type Wolf = { bark: () => void };
type Tiger = { purr: () => void };

type MyAnimal<G extends Genus> =
  G extends "canis" ?
    Wolf :
  G extends "felis" ?
    Tiger :
  never;

function getAnimal<G extends Genus>(
  genus: G,
  animal: MyAnimal<G>
) {
  // Narrow the type, so MyAnimal will yield Wolf!
  if (genus === "canis") {
    animal.bark();
    // ^?
    // ❓ The type is still Wolf | Tiger. Shouldn't it be Wolf?
  }
}

const wolf: Wolf = { bark: () => {} };

function main() {
  // ✅ However, the type narrowing works for the parameter when calling the function:
  getAnimal("canis", wolf);
  //                 ^?
}

🙁 Actual behavior

The type of a parameter is not narrowed in a conditional inside the function body based on a type variable and a generic type which depends on the type variable. However, where the function is actually called, its arguments are narrowed correctly based on the type variable and the generic type.

🙂 Expected behavior

I'd expect TypeScript to narrow the type in the conditional inside the function body based on the type variable and the generic type connected to it, as it can do the narrowing with the arguments when the function is called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions