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

TypeScript compile error #10

Closed
gucong3000 opened this issue Jan 22, 2018 · 2 comments · Fixed by #13
Closed

TypeScript compile error #10

gucong3000 opened this issue Jan 22, 2018 · 2 comments · Fixed by #13

Comments

@gucong3000
Copy link

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
Copy link
Author

gucong3000 commented Jan 22, 2018

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

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

gucong3000 added a commit to gucong3000/plugin-error that referenced this issue Jan 22, 2018
@gucong3000 gucong3000 changed the title TypeScript Compile error TypeScript compile error Jan 22, 2018
@demurgos
Copy link
Member

demurgos commented Jan 22, 2018

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.

gucong3000 added a commit to gucong3000/plugin-error that referenced this issue Jan 23, 2018
gucong3000 added a commit to gucong3000/plugin-error that referenced this issue Jan 23, 2018
gucong3000 added a commit to gucong3000/plugin-error that referenced this issue Jan 23, 2018
demurgos added a commit to demurgos/plugin-error that referenced this issue Jan 23, 2018
This commit updates the signature of the `PluginError` constructor to
loosen the constraints on the parameters in order to support union types
while keeping inference on custom properties.

This allows for example to use the pattern below:
```
function createPluginError(error: Error | string) {
  return new PluginError("test", error);
}
```
(The tests were updated with more complex cases)

See the discussions in gulpjs#10 and gulpjs#11 for details.

- Closes gulpjs#10
- Closes gulpjs#11

/cc @gucong3000
demurgos added a commit to demurgos/plugin-error that referenced this issue Jan 23, 2018
This commit updates the signature of the `PluginError` constructor to
loosen the constraints on the parameters in order to support union types
while keeping inference on custom properties.

This allows for example to use the pattern below:
```
function createPluginError(error: Error | string) {
  return new PluginError("test", error);
}
```
(The tests were updated with more complex cases)

See the discussions in gulpjs#10 and gulpjs#11 for details.

- Closes gulpjs#10
- Closes gulpjs#11

/cc @gucong3000
demurgos added a commit to demurgos/plugin-error that referenced this issue Jan 23, 2018
This commit updates the signature of the `PluginError` constructor to
loosen the constraints on the parameters in order to support union types
while keeping inference on custom properties.
This is a semver minor update (fix).

This allows for example to use the pattern below:
```
function createPluginError(error: Error | string) {
  return new PluginError("test", error);
}
```
(The tests were updated with more complex cases)

See the discussions in gulpjs#10 and gulpjs#11 for details.

- Closes gulpjs#10
- Closes gulpjs#11

/cc @gucong3000
demurgos added a commit to demurgos/plugin-error that referenced this issue Jan 23, 2018
This commit updates the signature of the `PluginError` constructor to
loosen the constraints on the parameters in order to support union types
while keeping inference on custom properties.
This is a semver minor update (fix).

This allows for example to use the pattern below:
```
function createPluginError(error: Error | string) {
  return new PluginError("test", error);
}
```
(The tests were updated with more complex cases)

See the discussions in gulpjs#10 and gulpjs#11 for details.

- Closes gulpjs#10
- Closes gulpjs#11

/cc @gucong3000
demurgos added a commit that referenced this issue Jan 23, 2018
This commit updates the signature of the `PluginError` constructor to
loosen the constraints on the parameters in order to support union types
while keeping inference on custom properties.
This is a semver minor update (fix).

This allows for example to use the pattern below:
```
function createPluginError(error: Error | string) {
  return new PluginError("test", error);
}
```
(The tests were updated with more complex cases)

See the discussions in #10 and #11 for details.

- Closes #10
- Closes #11

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

Successfully merging a pull request may close this issue.

2 participants