Search code examples
typescriptentitytypeorm

Argument of type 'User' is not assignable to parameter of type 'EntityTarget<BaseEntity>'


I'm sending an "Entity" to the function and using it inside the function. But it is giving me this error.

export type Entities = User | Blog | Service
import AppDataSource from '../database'
import { Entities } from '../entity/index'
import User from '../entity/User'

const getList = async (entity: Entities) => {
  const list = (await AppDataSource.manager.find(entity))
}

Argument of type 'Entities' is not assignable to parameter of type 'EntityTarget'. Type 'User' is not assignable to type 'EntityTarget'.

enter image description here


Solution

  • you need to extend from EntityTarget:

    export interface User extends EntityTarget<unknown> {}
    export interface Blog extends EntityTarget<unknown> {}
    export interface Service extends EntityTarget<unknown> {}
    
    export type Entities = User | Blog | Service
    
    import AppDataSource from '../database'
    import { Entities } from '../entity/index'
    import User from '../entity/User'
    
    const getList = async (entity: Entities) => {
      const list = (await AppDataSource.manager.find(entity))
    }