1

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();
I3artosz
  • 123
  • 1
  • 11
  • 1
    I think it might be your tsconfig, but you might want to ask in Ben's discord server – kelsny Mar 07 '22 at 14:21
  • I think the root of the problem is that since Ben recorded the tutorial you're watching there have been many new MikroORM releases. Starting from v5, em.create requires you to pass all non-optional properties. You should define ```createdAt``` and ```updatedAt``` as OptionalProps, instead of ```title```. See: [MikroORM documentation](https://mikro-orm.io/docs/upgrading-v4-to-v5#emcreate-respects-required-properties) and also a [related question on stack overflow](https://stackoverflow.com/questions/71013801/optionalprops-in-mikro-orm) – milancodes Mar 21 '22 at 10:01

3 Answers3

4

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

enter image description here

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Sam Morgan
  • 41
  • 2
  • Please post your code directly to the post, no need of adding extra URLs that can become invalid in future. – Tyler2P May 02 '22 at 11:07
2

I had same problem once, the thing is TS does not know the type of the object you are trying to pass it, and it is expeting an object of Type Defined in the first argument of orm.create(Post , ... ) Now there are multiple solutions for this problem:

1: Creating the entity data with following command and TS will infer the Type on it's own:

await orm.em.nativeInsert(Post, { name: "my second post" });

2: Write the type of Post by importing RequiredEntityData from the ORM itself

import { MikroORM, RequiredEntityData } from "@mikro-orm/core";
// make a post to the database
  const post = orm.em.create(Post, {
    name: "my first post",
  } as RequiredEntityData<Post>);

3: Simply write it's type as Post without importing anything from ORM:

// make a post to the database
  const post = orm.em.create(Post, {
    name: "my first post",
  } as Post);
Asad Ashraf
  • 1,425
  • 8
  • 19
1

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!