'Conditional type inference failing to handle a union with a Promise
I have a situation where I'm trying to infer a type parameter that might be wrapped in a Promise, and when I try to represent it as a union that includes Promise<NonNullable<T>>, the inference fails to narrow my param (it only works if I pass the entire union as the type parameter for NonNullable).
type OuterUnion<T> = NonNullable<T> | Promise<NonNullable<T>>;
type InnerUnion<T> = NonNullable<T | Promise<T>>
function usingOuter<T>(args: T): T extends OuterUnion<infer Y> ? Y | undefined : never {
return null as any
}
function usingInner<T>(args: T): T extends InnerUnion<infer Y> ? Y | undefined : never {
return null as any
}
// string | Promise<string> | undefined :(
const result1 = usingOuter(Promise.resolve('string'))
// string | undefined :)
const result2 = usingInner(Promise.resolve('string'))
I guess if I completely spell out the return type, I get a very strange looking infer inside a promise, that looks like T extends Promise<infer Y extends null | undefined ? never : infer Y> ? Y | undefined : never but I still don't understand the mechanics for why it fails.
Is there a direct reason for this? Thanks in advance for your help!
disclaimer: my actual use case is more complicated which is why I'm using conditional type inference in the first place (args is a variadic tuple where Y can be different types like string | number etc where the order matters). I tried to whittle it down to the simplest reproducible example which probably comes across very contrived here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
