Skip to content

TypeScript types error as unknown #29

@varanauskas

Description

@varanauskas

When trying to use this library

try {
  return await this.exampleRepository.save(input);
} catch (error) {
  const wrapped = wrapError(error);
  if (wrapped instanceof UniqueViolationError) {
    throw new ExampleAlreadyExists(wrapped);
  }
  throw error;
}

TypeScript complains because error is of type unknown rather than Error

const wrapped = wrapError(error);
                       // ^^^^^
                       // Argument of type 'unknown' is not assignable to parameter of type 'Error'.

Because of changes in TypeScript 4.4
https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#use-unknown-catch-variables

User needs to cast the error to Error, however, it does not seem that the code would fail if the error were any other type, so perhaps the typescript d.ts can be changed?

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