Cannot infer
the return type of a generic function in a conditional type
#58779
Labels
Duplicate
An existing issue was already created
🔎 Search Terms
Conditional type inference, generic return type, type-level lambda, higher-kinded types, mapped types
🕗 Version & Regression Information
Present in TS 3.5.1 to 5.5.0-beta.
⚪️ Introduction
I need to write a piece of code that would transverse a deeply nested data structure and fetch required data. However, I would prefer to do it in a way that doesn't force me to couple it too strongly to the data structure's specific type. This would be easiest to achieve with a generic replacer. Trying to implement it, I came across a very unintuitive behavior: given a generic function and its type parameter, it is impossible to correctly infer its return type.
💻 Code
⏯ Playground Link
🙁 Actual behavior
Does not infer the type argument, replaces it with
unknown
.🙂 Expected behavior
Should use the inferred type argument to correctly infer the return type:
string[]
.Additional information about the issue
It seems to me that adding inference here would be possible – after all if I actually call the function, the type argument does get infered, and so does the return type. The doors this would open are countless, as infering the return type of a generic function is essentially higher-kinded types in a trench coat.
Were this implemented, we could do things like these:
(Compare #27130)
Possibly Related Issues
#57351, #33594
The text was updated successfully, but these errors were encountered: