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

Remove as any from caseError #2

Closed
hrajchert opened this issue Aug 23, 2018 · 1 comment
Closed

Remove as any from caseError #2

hrajchert opened this issue Aug 23, 2018 · 1 comment

Comments

@hrajchert
Copy link
Contributor

hrajchert commented Aug 23, 2018

Currently the caseError implementation needs to return it's value as any cause we get an error of Task<TResult, EResult> is not assignable to the conditional type.

export function caseError<E, TResult, EResult>(
  errorType: Constructor<E>,
  errorHandler: IErrorHandler<E, TResult, EResult>
) {
  return function<RE>(
    err: RE
  ): RE extends E ? Task<TResult, EResult> : Task<TResult, RE | EResult> {
    // If the error is of the type we are looking for (E)
    if (err instanceof errorType) {
      // Transform the error
      return errorHandler(err) as any
    } else {
      // If not, leave as it is
      return Task.reject(err) as any
    }
  }
}

We have to investigate conditional types further, because this base test is currently failing trough
the same problem;

function test<T> (val: T): T extends number ? string : boolean {
  if (typeof val === 'number') {
    val; // This gets infer as T & number, instead of just number
    return 'string'; // this fails because string is not assignable to the conditional.
  } else {
    return false;
  }
}
hrajchert pushed a commit that referenced this issue Aug 27, 2018
* fix(caseError): fixed caseError typings (now works with non-compatible errors and with Tasks errores with only one type)

* fix(caseError): improve caseError typings (fix #2)

* chore(caseError): removed debugging code
@hrajchert
Copy link
Contributor Author

🎉 This issue has been resolved in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant