Vincit / objection.js

An SQL-friendly ORM for Node.js

Home Page:https://vincit.github.io/objection.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Export of error classes as values prevents use as types

mmmmmrob opened this issue · comments

DB Errors are exported from objection in objection/typings/objection/index.d.ts.

  const DBError: typeof dbErrors.DBError;
  const DataError: typeof dbErrors.DataError;
  …snip…
  const NotNullViolationError: typeof dbErrors.NotNullViolationError;

The errors are classes that could be used as types if exported differently but the export as value prevents use as a type.

import { UniqueViolationError } from 'objection'

export class UserExists extends Error {
  // typescript error:
  // 'UniqueViolationError' refers to a value, but is being used as a type here. Did you mean 'typeof UniqueViolationError' ts(2749)
  error: UniqueViolationError
}

Exporting the classes directly rather than as a variable would allow them to be used as types.

I'd be happy to put up a PR if someone is able to guide me on what's preferred here.

Is the "correct" way to deal with this to import the errors directly from db-errors instead of from objection?

This is resolved for me by importing the errors direct from db-errors.