gulpjs / plugin-error

Error handling for vinyl plugins. Just an abstraction of what's in gulp-util with minor reformatting.
MIT License
19 stars 13 forks source link

TypeScript compile error #10

Closed gucong3000 closed 6 years ago

gucong3000 commented 6 years ago
tsc
lib/eclint.ts(112,38): error TS2345: Argument of type 'string | Error' is not assignable to parameter of type 'Options & { message: string; }'.
  Type 'string' is not assignable to type 'Options & { message: string; }'.
    Type 'string' is not assignable to type '{ message: string; }'.

https://travis-ci.org/jedmao/eclint/jobs/331685076

gucong3000 commented 6 years ago
  function createPluginError(err: Error | string) {
    return new PluginError(PLUGIN_NAME, err);
  }

I don't know how to modify my code to compile

demurgos commented 6 years ago

Thanks for PR, it was needed indeed. I proposed a change to better support custom errors, could you update your branch?

If you want to move on with the current types, just use an any cast:

  function createPluginError(err: Error | string) {
    return new PluginError(PLUGIN_NAME, err as any);
  }

Or you could do a disjunction on the type of err, but this looks a bit silly (it's better to use any than get some convoluted code just to appease the type checker):

  function createPluginError(err: Error | string) {
    if (typeof err === "string") {
      return new PluginError(PLUGIN_NAME, err);
    } else {
      return new PluginError(PLUGIN_NAME, err);
    }
  }

I thought that TS would automatically resolve the correct result but the fact that one of the variants has a generic parameter type or that there are different return values may have confused it. Adding the corresponding signature manually is the best solution for the moment.