Encountered an issue when attempting to include a model in sequelize-typescript

I've been attempting to incorporate a model using sequelize-typescript:


type AppMetaDataAttributes = {
  id: string;
  name: string;
  version: string;
  createdAt: string;
  updatedAt: string;
};

type AppMetaDataCreationAttributes = Optional<AppMetaDataAttributes, 'id'>;

@Table
class AppMetaData extends Model<AppMetaDataAttributes, AppMetaDataCreationAttributes> {
  @Column
  id: string;

  @Column
  name: string;

  @Column
  version: string;

  @Column({ field: 'created_at' })
  createdAt: string;

  @Column({ field: 'updated_at' })
  updatedAt: string;

  constructor(id: string, name: string, version: string, createdAt: string, updated: string) {
    super();
    this.id = id;
    this.name = name;
    this.version = version;
    this.createdAt = createdAt;
    this.updatedAt = updated;
  }
}
MySqlInstance.addModels([AppMetaData]);

export default AppMetaData;

However, I encountered the following error in the line

MySqlInstance.addModels([AppMetaData]);
:

No overload matches this call.
  The last overload gave the following error.
    Type 'typeof AppMetaData' is not assignable to type 'string | ModelCtor<Model<any, any>>'.
      Type 'typeof AppMetaData' is not assignable to type 'ModelCtor<Model<any, any>>'.
        Type 'typeof AppMetaData' is not assignable to type 'new () => Model<any, any>'.
          Types of construct signatures are incompatible.
            Type 'new (id: string, name: string, version: string, createdAt: string, updated: string) => AppMetaData' is not assignable to type 'new () => Model<any, any>'.ts(2769)
sequelize.d.ts(16, 5): The last overload is declared here.

I followed the guidelines provided in the official documentation found here, but I'm unsure where I went wrong?

Answer №1

The constructor method in the AppMetaData model class serves no purpose.

Here is a practical example:

import { Optional } from 'sequelize';
import { Column, Model, Table, Sequelize } from 'sequelize-typescript';

const sequelize = new Sequelize({
  database: 'some_db',
  dialect: 'sqlite',
  username: 'root',
  password: '',
  storage: ':memory:',
  models: [__dirname + '/models'],
});

type AppMetaDataAttributes = {
  id: string;
  name: string;
  version: string;
  createdAt: string;
  updatedAt: string;
};

type AppMetaDataCreationAttributes = Optional<AppMetaDataAttributes, 'id'>;

@Table
class AppMetaData extends Model<
  AppMetaDataAttributes,
  AppMetaDataCreationAttributes
> {
  @Column
  id: string;

  @Column
  name: string;

  @Column
  version: string;

  @Column({ field: 'created_at' })
  createdAt: string;

  @Column({ field: 'updated_at' })
  updatedAt: string;
}
sequelize.addModels([AppMetaData]);

Check out this code on stackblitz

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am unable to employ filtering in TypeScript

Hey there, I'm trying to filter some JSON data randomly by using this function, but I keep running into an error with my variable called filteredArray. The error message says "Property 'filter' does not exist on type 'Dispatch<SetSta ...

Access network path through browser using Angular

I'm having trouble opening a network path from my browser using the code below. The browser keeps throwing an error saying it's unable to load local resources. Can you please provide some advice on this issue? public openUrl() { window.o ...

Refreshing local storage memory on render with a custom Next.js hook

I recently developed a custom Next.js hook named useLocalStorage to store data in local storage. Everything is working fine, except for one issue - the local storage memory gets refreshed with every render. Is there a way to prevent this from happening? ...

Issue encountered in Typescript: callback functions are returning undefined value when called from a superclass

As a newcomer to TypeScript and Node.js, I decided to dive into something new by exploring Angular, Node, and Express. While attempting to practice good project structure practices in Express by breaking it down into smaller parts, I encountered an issue. ...

Collaborate and reuse Typescript code across various Node projects

Imagine we have a project structured like this: webapps ProjectA SomeClass.ts Package.json ProjectB SomeClass.ts Package.json Common LoggingClass.ts Package.json The Common "LoggingClass" needs to import a module from NPM. Let's say that ...

How can the ordering of dynamically generated components be synchronized with the order of other components?

Currently, I'm delving into Vue 3 and encountering a specific issue. The tabs library I'm using only provides tab headers without content panels. To work around this limitation, I've come up with the following solution: <myTabs/><!- ...

The Chrome debugger fails to display variable values when hovering the mouse over them

After creating a basic React app using the command "npx create-react-app my-app --template typescript", I encountered an issue where the values were not appearing in Chrome dev tools when I added a breakpoint in the code. Is this expected behavior for a Re ...

Do [(ngModel)] bindings strictly adhere to being strings?

Imagine a scenario where you have a radiobutton HTML element within an angular application, <div class="radio"> <label> <input type="radio" name="approvedeny" value="true" [(ngModel)]=_approvedOrDenied> Approve < ...

Having trouble navigating typescript's "import" syntax in conjunction with compiler options like module and choosing between esnext and commonjs?

I am facing an issue with my typescript project that includes mocha tests. Here is a snippet of how the tests start: import { assert } from "chai"; import "@material/mwc-ripple"; //I need to test a script that uses this describe("simple test", () => { ...

Struggling to make type definitions work in react-hook-form 7

Upon defining the types of my form fields, I encountered an issue where each field seems to take on all three different types. This is puzzling as I have specified 3 distinct types for my 3 different fields. What could be causing this confusion? https://i ...

`Implementing Typescript code with Relay (Importing with System.js)`

Is there a way to resolve the error by including system.js or are there alternative solutions available? I recently downloaded the relay-starter-kit (https://github.com/relayjs/relay-starter-kit) and made changes to database.js, converting it into databas ...

What benefits does Observable provide compared to a standard Array?

In my experience with Angular, I have utilized Observables in the state layer to manage and distribute app data across different components. I believed that by using observables, the data would automatically update in the template whenever it changed, elim ...

Connecting an Angular 4 Module to an Angular 4 application seems to be causing some issues. The error message "Unexpected value 'TestModule' imported by the module 'AppModule'. Please add a @NgModule annotation" is

Update at the bottom: I am currently facing a massive challenge in converting my extensive Angular 1.6 app to Angular 4.0. The process has turned into quite a formidable task, and I seem to be stuck at a specific point. We have a shared set of utilities th ...

What might be causing the component in Angular and TypeScript to have trouble reading the data returned by the service?

I've been working on this for hours without success. I created a web API get method that returns a simple array. public Hero[] getHeroes() { return new Hero[] { new Hero { Id = 1, Name = "Hunain Hafeez-1" }, ...

There is a WARNING occurring at line 493 in the file coreui-angular.js located in the node_modules folder. The warning states that the export 'ɵɵdefineInjectable' was not found in the '@angular/core' module

I encountered a warning message while running the ng serve command, causing the web page to display nothing. Our project utilizes the Core Ui Pro Admin Template. Here is the list of warning messages: WARNING in ./node_modules/@coreui/angular/fesm5/coreu ...

typescript decorator implemented on a class that is nested within another class

Is it possible to decorate a nested property within a class? Let's explore with an example: function log(name: string = 'DecoratedProp') { return function logHandler(target: any, field: any) { // get the key ...

Having trouble retrieving the parent object in Angular OnInit method?

I am facing an issue with attaching a custom validator to an Angular Form Control. The Form Controls are initialized in the ngOnInit method, and I want the validator to check if a field has input only when a boolean class member this.shouldCheck is true. ...

Another query on Promises - how to handle nested Sequelize calls?

After researching extensively about Promises, I have a good understanding of how they work and have been implementing them for quite some time. However, I've come across a specific scenario that I can't seem to find a clear solution for in all th ...

How can Material UI Textfield be configured to only accept a certain time format (hh:mm:ss)?

Looking for a way to customize my material ui textfield to allow input in the format of hh:mm:ss. I want to be able to adjust the numbers for hours, minutes, and seconds while keeping the colons automatic. Any suggestions would be welcomed. ...

Application built with Electron and Typescript encounters module-related crash

While working on developing a client using Electron with Typescript, I encountered the following error: The configuration in tsconfig.json looks like this: { "compilerOptions": { "target": "es5", "lib": [ ...