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

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

Closed
karex opened this issue Jan 20, 2023 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@karex
Copy link

karex commented Jan 20, 2023

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.

@MartinJohns
Copy link
Contributor

Within the function the compiler doesn't know what type G is, so it can't resolve your conditional type. It's a design limitation. Seen this issue so super often already, I should write an entry for the FAQ..

@whzx5byb
Copy link

whzx5byb commented Jan 20, 2023

The type is still Wolf | Tiger. Shouldn't it be Wolf?

It seems that you expect the CFA to narrow G to either one branch of Genus, but remember G could still be a union when invoked with an explicit generic type: getAnimal<Genus>("canis", { purr: () => {} })

And here is a workaround to make your expected behavior:

function getAnimal2<G extends Genus>(
  ...[ genus, animal ]: G extends unknown ? [ genus: G, animal: MyAnimal<G> ] : never
) {
  if (genus === "canis") {
    animal.bark();
    // ^? animal: Wolf
  } else {
    animal.purr();
    // ^? animal: Tiger
  }
}

getAnimal2("canis", { bark: () => {} }) // OK
getAnimal2("felis", { purr: () => {} }) // OK
getAnimal2<Genus>("canis", { purr: () => {} }) // Error as expected

Related discussion: #27808

@fatcerberus
Copy link

To expand on the above comment, this is a legal call for the code as written in the OP:

const wolf: Wolf = { bark: () => {} };
const genus: Genus = "felis";
function main() {
  getAnimal(genus, wolf);
}

So you don't even need to explicitly supply the type argument to make the call unsound.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 23, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

6 participants