Search code examples
typeorm

TypeORM won't accept entity


When creating a TypeORM entity in my express + typescript project I got hit with this error:

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.

This is the code (copy pasted from TypeORM docs)

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    firstName: string

    @Column()
    lastName: string

    @Column()
    age: number
}

all the properties also get this error: Property 'age' has no initializer and is not definitely assigned in the constructor

I have no clue what is going on since it's a clear paste and I have used typeorm with nestjs before without any issues


Solution

  • The issue was inside the tsconfig as I have forgot to enable decorators in my config as well as disable strictPropertyInitialization

    Here is the solution in code:

    tsconfig.json:

    ...
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    ...