Skip to content

Typescript Compiler confused about Knex import in Mixin Typings #1770

@Zuzuesque

Description

@Zuzuesque

Objection 2.1.5
Typescript 3.9.3

Issue:
When using mixins in Typescript the compiler gets confused about the 'Knex' type, throwing the following error: Return type of exported function has or is using name 'Knex' from external module [...] but cannot be named.ts(4058)

How to reproduce

import { Model } from 'objection';

export function Mixin(options = {}) {
  return function<T extends typeof Model>(Base: T) {
    return class extends Base {
      mixinMethod() {}
    };
  };
}

Taking the Mixing example from the homepage, dropping it into a fresh project. (Note: the example appears to have other issues, at least in TS 3.9.3, but those I can solve... the Knex error however won't go away no matter what I do.) For all other purposes, typings appear to work, however.

Experimenting a little with Objections typings I could solve the issue by replacing:

import * as knex from 'knex';
import Knex = require('knex');

in index.d.ts with:

import knex from 'knex'

In my project that solved the issue with the Knex error without adding any others... but it is a little weird. Though I guess the second import is a strange combo of commonJS and ES Modules... so that may throw the compiler?

Further Info
Some background to the issue: microsoft/TypeScript#5711

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