'Argument of type '{ title: string; }' is not assignable to parameter of type 'RequiredEntityData<Post>'

I'm learning from Ben's Awad video: Fullstack React GraphQL TypeScript Tutorial. I ran into a problem with typescript.

enter image description here

I don't understand why compiler thinks that Post is type of {title:string}..

My code:

import { Entity, PrimaryKey, Property, OptionalProps } from '@mikro-orm/core';
import { Field, Int, ObjectType } from 'type-graphql';

@ObjectType()
@Entity()
export class Post {
  [OptionalProps]?: 'title'; // id is there automatically

  @Field()
  @PrimaryKey()
  _id!: number;

  @Field(() => String)
  @Property()
  createdAt: Date = new Date();

  @Field(() => String)
  @Property({ onUpdate: () => new Date() })
  updatedAt: Date = new Date();

  @Field(() => String)
  @Property()
  title?: string;

My app.ts

const main = async () => {
    const orm = await MikroORM.init(microConfig);
    await orm.getMigrator().up();
    const post = orm.em.create(Post, {title: 'new post'}); // here is error
    await orm.em.persistAndFlush(post);
}

main();


Solution 1:[1]

Just type the 'data' property of the create method as RequiredEntityData<Post> like this:

enter image description here

Solution 2:[2]

After export, make sure to use OptionalProps to other entities as well and not just title.

  [OptionalProps]?: "title" | "updateAt" | "createdAt";

You don't need to add id as optional because we want id to be required and as a primary key.

That should fix the error!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P
Solution 2 Nitish Kafle