typescripttypescript-typingsmikro-orm

I want to init mikroConfig to MikroORM with typescript and i got this error message


The error message:

Argument of type '{ readonly entities: readonly [typeof Post]; readonly dbName: "lireddit"; readonly type: "postgresql"; readonly debug: boolean; }' is not assignable to parameter of type 'Configuration<IDatabaseDriver> | Options<IDatabaseDriver> | undefined'. Type '{ readonly entities: readonly [typeof Post]; readonly dbName: "lireddit"; readonly type: "postgresql"; readonly debug: boolean; }' is not assignable to type 'Options<IDatabaseDriver>'. Type '{ readonly entities: readonly [typeof Post]; readonly dbName: "lireddit"; readonly type: "postgresql"; readonly debug: boolean; }' is not assignable to type 'Partial<MikroORMOptions<IDatabaseDriver>>'. Types of property 'entities' are incompatible. The type 'readonly [typeof Post]' is 'readonly' and cannot be assigned to the mutable type '(string | EntityClass<AnyEntity> | EntityClassGroup<AnyEntity> | EntitySchema<any, undefined>)[]'.ts(2345)

The index.ts:

import { MikroORM } from '@mikro-orm/core';
import { __prod__ } from './constants';
import { Post } from './entities/Post';
import mikroConfig from './mikro-orm.config';

const main = async() => {
    const orm = await MikroORM.init(mikroConfig);
    const post = orm.em.create(Post, {title:'ez az első posztom hehe'})
    await orm.em.persistAndFlush(post)
}

main().catch((err) => {
    console.error(err)
})

And the mikro-orm.config.ts:

import { Post } from "./entities/Post";
import { __prod__ } from "./constants";

export default {
    entities:[Post],
    dbName: "lireddit",
    type: "postgresql",
    debug : !__prod__,
} as const;

Thank you for the help i you can, its so painful


Solution

  • 2023 update - there is a defineConfig helper nowadays, which is better suited:

    // import it from your driver package and you 
    // won't need to provide the `type` or `driver` option
    import { defineConfig } from '@mikro-orm/postgresql';
    import { Post } from "./entities/Post";
    import { __prod__ } from "./constants";
    
    export default defineConfig({
        entities: [Post],
        dbName: "lireddit",
    //  type: "postgresql", not needed with `defineConfig()`
        debug : !__prod__,
    });
    

    The way you are defining your ORM config is wrong, you should use Options type from the core package instead of const assertion. Define the config this way to have the best intellisense support (as well as to get rid of that TS error):

    import { Options } from '@mikro-orm/core';
    import { Post } from "./entities/Post";
    import { __prod__ } from "./constants";
    
    const config: Options = {
        entities: [Post],
        dbName: "lireddit",
        type: "postgresql",
        debug : !__prod__,
    };
    export default config;