Callsite inference of a conditional string literal type fails when the literal type of the parameter would produce a valid expansion #59829
Labels
Duplicate
An existing issue was already created
This is a bit of a weird one, bear with me...
As a hobby project/exercise, I've been working on (parts of) a literal regex engine using recursive TypeScript types. It is flatly impossible to do much of interest along these lines with first-class types, so instead I've been using conditional types to write validators that either resolve to
never
or to identity. This allows validation of literal function params, like a sort of SFINAE:While mucking around with this, I found myself in a situation where TSC fails to infer such a parameter type in situations where the literal type of a provided string literal argument produces a valid expansion. Instead, it falls back to the broader string type, which fails to expand.
I've extracted a repro case here. Curiously, the behavior depends on the order of the recursive branches in the parameter type; if we swap the success check to the beginning of the type, the inference works as expected.
The relevant change between the two:
Note that in both cases, the
ResolveQuantifiers
type itself seems to behave as we would like, and if we provide a copy of the parameter as an explicit generic argument the compilation error goes away.This seems like an unintended and undesirable behavior. Naively, I'd expect the inference engine to check the exact literal type of a literal parameter first, and infer the resulting generic if it evaluates to something other than
never
. What's going on here?The text was updated successfully, but these errors were encountered: