Description
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.