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

Generated typescript migrations and seeds have type errors #2808

Closed
nhjk opened this issue Sep 15, 2018 · 5 comments
Closed

Generated typescript migrations and seeds have type errors #2808

nhjk opened this issue Sep 15, 2018 · 5 comments

Comments

@nhjk
Copy link

nhjk commented Sep 15, 2018

Environment

Knex version: 0.15.2

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do

Getting type errors, shouldn't have any.

  1. Error message
Type 'Bluebird<any>' is not assignable to type 'Promise<any>'.
  Property '[Symbol.toStringTag]' is missing in type 'Bluebird<any>'.

This is because the bluebird promise type is incompatible with the typescript Promise type. This can fixed by simply wrapping the function body in Promise.resolve(...) and returning that instead.

Example (seed file, needs to be fixed with migration as well)

Current generated code

import * as Knex from "knex";

exports.seed = function (knex: Knex): Promise<any> {
    // Deletes ALL existing entries
    return knex("table_name").del()
        .then(function () {
            // Inserts seed entries
            return knex("table_name").insert([
                { id: 1, colName: "rowValue1" },
                { id: 2, colName: "rowValue2" },
                { id: 3, colName: "rowValue3" }
            ]);
        });
};

Code that typechecks correctly by adding Promise.resolve(...)

import * as Knex from "knex";

exports.seed = function(knex: Knex): Promise<any> {
  // Deletes ALL existing entries
  return Promise.resolve( // <-- added line
    knex("table_name")
      .del()
      .then(function() {
        // Inserts seed entries
        return knex("table_name").insert([
          { id: 1, colName: "rowValue1" },
          { id: 2, colName: "rowValue2" },
          { id: 3, colName: "rowValue3" }
        ]);
      })
  );
};
@elhigu
Copy link
Member

elhigu commented Sep 17, 2018

Wouldnt it be better to declare function as async?

exports.seed = async function(knex: Knex): Promise<any> {
  //...
};

@nhjk
Copy link
Author

nhjk commented Sep 18, 2018

Yes that's what I started doing shortly after filing the issue, should have updated it.

@JakeGinnivan
Copy link
Contributor

Could also use PromiseLike

ie

import * as Knex from "knex";

exports.seed = function (knex: Knex): PromiseLike<any> {
    // Deletes ALL existing entries
    return knex("table_name").del()
        .then(function () {
            // Inserts seed entries
            return knex("table_name").insert([
                { id: 1, colName: "rowValue1" },
                { id: 2, colName: "rowValue2" },
                { id: 3, colName: "rowValue3" }
            ]);
        });
};

@elhigu
Copy link
Member

elhigu commented Oct 1, 2018

Fixed #2816

@elhigu elhigu closed this as completed Oct 1, 2018
@meghna2109
Copy link

(function (exports, require, module, __filename, __dirname) { import ...
getting error - on import statement.

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

No branches or pull requests

4 participants