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

Issue with imports - TypeScript < 4 #498

Closed
Davste93 opened this issue Feb 10, 2021 · 10 comments
Closed

Issue with imports - TypeScript < 4 #498

Davste93 opened this issue Feb 10, 2021 · 10 comments

Comments

@Davste93
Copy link

Davste93 commented Feb 10, 2021

image

Is anyone using this with TypeScript?

Not sure why the generated code is doing:

import Sequelize, { DataTypes, Model, Optional } from 'sequelize';

static initModel(sequelize: Sequelize.Sequelize): 

Instead of:

import { Sequelize, DataTypes, Model, Optional } from 'sequelize';

static initModel(sequelize: Sequelize): 

I don't know if Sequelize went from default to named exports, but it seems that this is not working as per sequelize 6.5.0.

@Davste93
Copy link
Author

To anyone looking for a quick workaround:

const replace = require('replace-in-file');
...
const auto = new SequelizeAuto(...);
auto.run().then(data => {
  replace.sync({
    ...replaceConfig,
    from: /import Sequelize, \{ DataTypes, Model, Optional \}/g,
    to: 'import { Sequelize, DataTypes, Model, Optional }',
  });

  replace.sync({
    ...replaceConfig,
    from: /Sequelize.Sequelize/g,
    to: 'Sequelize',
  });
});

@steveschmitt
Copy link
Collaborator

You need TypeScript 4.x. The syntax

import Sequelize, { DataTypes, Model, Optional } from 'sequelize';

imports the Sequelize namespace, which is used later in the mixin declarations:

  getCustomer!: Sequelize.BelongsToGetAssociationMixin<Customer>;

@steveschmitt steveschmitt changed the title Issue with imports Issue with imports - TypeScript < 4 Feb 23, 2021
@Davste93
Copy link
Author

Davste93 commented Feb 25, 2021

image

Typescript 4.0.5, both in IDE and package.json, as well as node modules.

@steveschmitt
Copy link
Collaborator

Hmm, let me try again to reproduce the problem.

@steveschmitt
Copy link
Collaborator

No luck. I've tried with Sequelize 6.3.5 and 6.5.0. I've tried with TypeScript 4.0.5 and 4.2.2.

The generated code works for me, while your workaround code gives me an error:

models/product.ts:49:20 - error TS2702: 'Sequelize' only refers to a type, but is being used as a namespace here.

My tsconfig.json looks like:

{
  "compilerOptions": {
    "allowJs": false,
    "checkJs": false,
    "strict": true,
    "declaration": false,
    "module": "commonjs",
    "noImplicitAny": true,
    "esModuleInterop": true,
    "preserveConstEnums": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "ES6",
    "skipLibCheck": true,
    "typeRoots": [
      "node_modules/@types"
    ]
  }
}

@awcchungster
Copy link

awcchungster commented Mar 7, 2021

I can replicate this issue:

"typescript": "^4.1.5"

The "quick workaround" #498 (comment) didn't work for me.

@awcchungster
Copy link

I found a resolution by changing my tsconfig

"allowSyntheticDefaultImports": true

@steveschmitt
Copy link
Collaborator

I think it works for me because I have

"esModuleInterop": true,

in my tsconfig.json.

@Davste93 can you try that?

@cesar3030
Copy link

To anyone looking for a quick workaround:

const replace = require('replace-in-file');
...
const auto = new SequelizeAuto(...);
auto.run().then(data => {
  replace.sync({
    ...replaceConfig,
    from: /import Sequelize, \{ DataTypes, Model, Optional \}/g,
    to: 'import { Sequelize, DataTypes, Model, Optional }',
  });

  replace.sync({
    ...replaceConfig,
    from: /Sequelize.Sequelize/g,
    to: 'Sequelize',
  });
});

Thanks for the lead, I had to do a few changes to have it working:

replace.sync({
  files: './src/models/*.ts',
  from: /import Sequelize, \{ DataTypes, Model, Optional \} from 'sequelize';/g,
  to: "import { DataTypes, Model, Optional } from 'sequelize';\nimport * as Sequelize from 'sequelize';"
});

@steveschmitt
Copy link
Collaborator

Fixed in 0.8.2

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