Support Awaited<T>
solidsnakedev opened this issue · comments
Jonathan commented
I have the following promise race condition where I'd like ts patterns to work as follows:
export async function timeoutAsync<T>(
asyncFunction: () => Promise<T>,
timeoutMs: number
): Promise<Result<T>> {
const race =
await
Promise.race([
asyncFunction(),
setTimeout(timeoutMs, new Error("timeout async")),
])
return match(race)
.returnType<Result<T>>()
.with(P.instanceOf(Error), (error) => ({type:"error", error: error}))
.otherwise((result) => ({type:"ok", data: result}))
}
somehow ts-patterns can not infer that Awaited is just T
'T' could be instantiated with an arbitrary type which could be unrelated to 'Exclude<Error, InvertPatternForExclude<Chainable<GuardP<unknown, Error>>, WithDefault<ExtractPreciseValue<Error | Awaited<T>, InvertPattern<Chainable<GuardP<unknown, Error>>, Error | Awaited<...>>>, Error | Awaited<...>>>> | Exclude<...>'.ts(2322)
Describe the solution you'd like
the only way I can return the correct types is as follows:
export async function timeoutAsync<T>(
asyncFunction: () => Promise<T>,
timeoutMs: number
): Promise<Result<T>> {
const race =
await
Promise.race([
asyncFunction(),
setTimeout(timeoutMs, new Error("timeout async")),
])
if (race instanceof Error){
return ({type: "error", error: race})
}
else {
return ({type: "ok", data: race})
}