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

Dealing with JSON data in the format of `(Object object)` requires a specific approach

I originally encountered object object when attempting to display JSON API data in HTML. I then used keyvalue in *ngFor which allowed me to display the object, but I am wondering how I can access and display the entire JSON data? Here are the relevant cod ...

Converting <reference path/> directive to ESM import: A step-by-step guide

As I embark on developing a TypeScript application, I've reached the realization that I am not a fan of using the <reference path /> triple-slash directive. Instead, I prefer utilizing import 'something'. However, every time I attempt ...

What steps do I need to take to create a fresh interface in useState with the help of Typescript

I'm attempting to replicate an input by utilizing useState with an interface. Each time I click on the + button, the interface should be duplicated in the state, thereby duplicating my input. Here is the code I am working on: interface newInputsInter ...

The perplexing actions of Map<string, string[]> = new Map() have left many scratching their heads

I encountered an issue while trying to add a value to a map in my Angular project. The map is initially set up using the following code: filters: Map<string, string[]> = new Map(); However, when I attempt to add a value to this map, it starts displa ...

Converting a React Typescript project to Javascript ES5: A step-by-step guide

I have a react typescript project and I need to convert the source code (NOT THE BUILD) to ES3 or ES5 JavaScript. This is because I want to use this code as a component in another React app. Can you suggest which preset and plugins I should use for this t ...

Unable to access property value following AJAX call

Here is my code snippet: constructor(props: any) { super(props); this.state = { list: [], }; } public componentWillMount() { this.loadData(); } public loadData = () => { axios.get(someURL) .then((response) = ...

Incorporating a complex React (Typescript) component into an HTML page: A Step-by

I used to have an old website that was originally built with Vanilia Javascript. Now, I am in the process of converting it to React and encountering some issues. I am trying to render a compound React(Typescript) component on an HTML page, but unfortunatel ...

What could be the reason for a property going unnoticed during the iteration of a list?

The Scenario There is a class named myClass: export class myClass { name: string; age: number; city: string; } and another class called people: export class people { name: string; age: number; } In the component.ts, a variable list ...

Text in Angular vanishes upon reopening

I have a code snippet where I am trying to allow the user to edit and save a paragraph displayed on a side panel of the main page. Although the code works fine, allowing users to update the text and see it reflected in the network upon saving, there seems ...

Transforming Boolean data types into text within an Angular 2 client-side application

Query I'm currently working on retrieving data from an API and displaying it in a table. One of the columns includes a status attribute that returns either true or false values. However, I would like to display "Active" or "Block" instead on the clie ...

Top Recommendations: Comparing Standalone Components and Modules in Angular Version 14

I'm in need of some clarification on the most effective practices when it comes to utilizing standalone components and modules within Angular 14. With the introduction of standalone components as a new concept in Angular, I am seeking factual guidance ...

I am experiencing issues with my Jest unit test case for Material UI's multi select component failing

I've been working on writing a unit test case for the material UI multi select component. The code for the parent component is as follows: import {myData} from '../constant'; export const Parent = () => { const onChangeStatus= (sel ...

The user interface is not being refreshed in the select box after removing control from the reactive form

Within my project, I am utilizing "@angular/cli": "1.2.6", "@angular/core": "^4.0.0" Objective My goal is to create a dynamic form for a product that includes feature inputs. When the user clicks the "add feature" button, a new feature column with a sel ...

Expanding function parameter types using intersection type in Typescript

As I delve into the world of intersection types to enhance a function with an incomplete definition, I encountered an interesting scenario. Take a look at this code snippet: WebApp.connectHandlers.use("/route", (req:IncomingMessage, res:ServerResponse)=& ...

Error message in Typescript with React: "The type 'ComponentClass<StyledComponentProps<{}>>' cannot be assigned to type 'typeof MyComponent'"

Currently experimenting with integrating the Material UI 1.0 (beta) @withStyles annotation into a React component. The documentation provides a JavaScript example (), however, it results in a compilation error when using Typescript. Despite the error, the ...

Exploring the integration of multiple HTTP requests in Angular with the power of RxJS

Is there a way to make multiple HTTP calls simultaneously in an Angular service and then combine the responses into one object using RxJS? import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; im ...

The value from select2 dropdown does not get populated in my article in Angular

I am attempting to link the selected value in a dropdown menu to an article, with a property that matches the type of the dropdown's data source. However, despite logging my article object, the property intended to hold the selected dropdown value app ...

What is the best way to implement a scroll event in a React application?

Can someone provide guidance on how to properly write the scrollHandler function for this scenario? ` useEffect(() => { document.addEventListener('submit', scrollHandler); return () => document.removeEventListener('scroll', ...

Patiently waiting for the component variable to be assigned through subscription

I am facing an issue with two calls in my component. The second call depends on the result from the first call. In the first call, I set the value for my component variable "locked". The second call should only be executed when the result is true, meaning ...

Using the HTTP Post method to retrieve a file object: a step-by-step guide

Is there a way to utilize a http POST request in order to retrieve a file object? Though the uploading of files to the server using the POST request seems successful and flawless, attempting to fetch the file results in an unusual response: console output ...