I'm trying to get sequelize-typescript decorators to compile with ts-node. I have created the following model for a user:

import {
  AutoIncrement,
  Column,
  DataType,
  Model,
  PrimaryKey,
  Table,
} from "sequelize-typescript";

@Table
export class User extends Model<User> {
  @PrimaryKey
  @AutoIncrement
  @Column(DataType.INTEGER)
  id!: number;

  @Column(DataType.STRING)
  username!: string;

  @Column(DataType.STRING)
  passwordHash!: string;

  @Column(DataType.STRING)
  email!: string;
}

VSCode doesn't complain about anything, but ts-node gives the following errors for @Table, @PrimaryKey and @AutoIncrement when I try to compile:

/app/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/models/User.ts(10,2): error TS1238: Unable to resolve signature of class decorator when called as an expression.
   The runtime will invoke the decorator with 2 arguments, but the decorator expects 1.
src/models/User.ts(10,2): error TS1270: Decorator function return type 'Function' is not assignable to type 'void | typeof User'.
   Type 'Function' is missing the following properties from type 'typeof User': isInitialized, initialize, tableName, primaryKeyAttribute, and 71 more.
src/models/User.ts(12,4): error TS1240: Unable to resolve signature of property decorator when called as an expression.
   Argument of type 'ClassFieldDecoratorContext<User, number> & { name: "id"; private: false; static: false; }' is not assignable to parameter of type 'string'.
src/models/User.ts(13,4): error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Argument of type 'ClassFieldDecoratorContext<User, number> & { name: "id"; private: false; static: false; }' is not assignable to parameter of type 'string'.
...

Here's my tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strictPropertyInitialization": false
  }
}

And here's my package.json (running nodemon on Node 18 LTS):

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon --exec ts-node ./src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "morgan": "^1.10.0",
    "pg": "^8.11.3",
    "reflect-metadata": "^0.2.1",
    "sequelize": "^6.35.2",
    "sequelize-typescript": "^2.1.6",
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3"
  },
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/morgan": "^1.9.9",
    "@types/node": "^20.10.5",
    "@types/validator": "^13.11.7",
    "nodemon": "^3.0.2"
  }
}

What am I doing wrong here?

0

There are 0 best solutions below