sindresorhus / serialize-error

Serialize/deserialize an error into a plain object
MIT License
538 stars 63 forks source link

Wrong typescript declaration for errorConstructors #78

Open ghost opened 2 years ago

ghost commented 2 years ago
Argument of type '(string | SystemError | AssertionError | EvalErrorConstructor | { new (message?: string | undefined, name?: string | undefined): DOMException; ... 25 more ...; readonly WRONG_DOCUMENT_ERR: number; })[][]' is not assignable to parameter of type 'Iterable<readonly [string, ErrorConstructor]>'.
      The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
        Type 'IteratorResult<(string | SystemError | AssertionError | EvalErrorConstructor | { new (message?: string | undefined, name?: string | undefined): DOMException; ... 25 more ...; readonly WRONG_DOCUMENT_ERR: number; })[], any>' is not assignable to type 'IteratorResult<readonly [string, ErrorConstructor], any>'.
          Type 'IteratorYieldResult<(string | SystemError | AssertionError | EvalErrorConstructor | { new (message?: string | undefined, name?: string | undefined): DOMException; ... 25 more ...; readonly WRONG_DOCUMENT_ERR: number; })[]>' is not assignable to type 'IteratorResult<readonly [string, ErrorConstructor], any>'.
            Type 'IteratorYieldResult<(string | SystemError | AssertionError | EvalErrorConstructor | { new (message?: string | undefined, name?: string | undefined): DOMException; ... 25 more ...; readonly WRONG_DOCUMENT_ERR: number; })[]>' is not assignable to type 'IteratorYieldResult<readonly [string, ErrorConstructor]>'.
              Type '(string | SystemError | AssertionError | EvalErrorConstructor | { new (message?: string | undefined, name?: string | undefined): DOMException; ... 25 more ...; readonly WRONG_DOCUMENT_ERR: number; })[]' is not assignable to type 'readonly [string, ErrorConstructor]'.
                Target requires 2 element(s) but source may have fewer.
  Overload 2 of 4, '(entries?: readonly (readonly [string, ErrorConstructor])[] | null | undefined): Map<string, ErrorConstructor>', gave the following error.
    Argument of type '(string | SystemError | AssertionError | EvalErrorConstructor | { new (message?: string | undefined, name?: string | undefined): DOMException; ... 25 more ...; readonly WRONG_DOCUMENT_ERR: number; })[][]' is not assignable to parameter of type 'readonly (readonly [string, ErrorConstructor])[]'.
      Type '(string | SystemError | AssertionError | EvalErrorConstructor | { new (message?: string | undefined, name?: string | undefined): DOMException; ... 25 more ...; readonly WRONG_DOCUMENT_ERR: number; })[]' is not assignable to type 'readonly [string, ErrorConstructor]'.

This message is shown when the code is converted to Typescript.

fregante commented 2 years ago

Where are you seeing this error? What's your code?

ghost commented 2 years ago

@fregante I have converted the code to Typescript

https://gist.github.com/ayanamidev/e32ea8edb5c4bd462b04ab30af9465cb

Since the type definitions are written & updated manually, I think the error from typescript compiler makes sense.

fregante commented 2 years ago

I thought I commented here. In short the error doesn't affect the final user, but PR welcome to fix the type to something more generic like new (message: string, ...args: unknown[]): Error. The issue here is that some internal Errors don't match the Error constructor exactly

fregante commented 2 years ago

I'm also having trouble adding my own constructor to this. It seems that:

Argument of type 'typeof MessengerError' is not assignable to parameter of type 'ErrorConstructor'. Type 'typeof MessengerError' provides no match for the signature '(message?: string | undefined): Error'.

Seen in https://github.com/pixiebrix/webext-messenger/blob/0ccfd3b008f5cb4d77bd993361022d041b66dcd8/source/shared.ts#L29-L34

Note: The line seems to work, it's just that the types seem incorrect.

barbu-vlad commented 1 year ago

Hi, I updated to the latest version of serialize-error (11.0.0) and the support for ios 13.6 and below is gone. Getting the following error. Looks related to error constructor:

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#field_declarations

fregante commented 1 year ago

That's unrelated. You'll just have to use Babel or keep using the older version.

EHadoux commented 2 months ago

Argument of type 'typeof MessengerError' is not assignable to parameter of type 'ErrorConstructor'. Type 'typeof MessengerError' provides no match for the signature '(message?: string | undefined): Error'.

Sorry to necro bump, I've got the exact same error (currently switched off with a ts-ignore comment). Any update on this?