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'.
CodePudding user response:
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))
}
CodePudding user response:
I got such an error in the code above "'X' type argument cannot be assigned to 'X' type parameter" I did it this way to solve the problem:
import { EntityTarget } from 'typeorm'
import AppDataSource from '../database'
export type _User = EntityTarget<'User'>
export type _Blog = EntityTarget<'Blog'>
export type _Service = EntityTarget<'Service'>
export type Entities = _User | _Blog | _Service
export const getList = async (entity: Entities) => {
const list = (await AppDataSource.manager.find(entity))
}
But I'm not sure if it's healthy
CodePudding user response:
I solved the problem this way, it seems more logical. Thanks
import { EntityTarget } from 'typeorm'
import AppDataSource from '../database'
const getList = async <E>(entity: EntityTarget<E>) => {
const list = await AppDataSource.manager.find(entity)
}