Skip to content

TypeScript compile error  #10

Closed

Description

@gucong3000
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

Activity

  1. gucong3000 commented on Jan 22, 2018

    @gucong3000
    Author
      function createPluginError(err: Error | string) {
        return new PluginError(PLUGIN_NAME, err);
      }

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

  2. added a commit that references this issue on Jan 22, 2018
    1f850c3
  3. changed the title [-]TypeScript Compile error [/-] [+]TypeScript compile error [/+] on Jan 22, 2018
  4. demurgos commented on Jan 22, 2018

    @demurgos
    Member

    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.

  5. added 3 commits that reference this issue on Jan 23, 2018
    87d0b3e
    bd5651c
    dc76155
  6. added a commit that references this issue on Jan 23, 2018
    eae2f4c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions