Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Awaited<T> #195

Open
solidsnakedev opened this issue Sep 27, 2023 · 0 comments
Open

Support Awaited<T> #195

solidsnakedev opened this issue Sep 27, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@solidsnakedev
Copy link

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})
  }
@solidsnakedev solidsnakedev added the enhancement New feature or request label Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant