I am trying to using the Factory Pattern to create DAO. Here is my code:
interface IBaseDao {
findOne(): Promise<any>;
}
abstract class BaseDao implements IBaseDao {
public async findOne() {}
}
interface IUserDao extends IBaseDao {
findByEmail(email: string): Promise<any>;
}
class UserDaoImpl extends BaseDao implements IUserDao {
public async findByEmail(email: string) {}
}
interface IPostDao extends IBaseDao {
findByTitle(title: string): Promise<any>;
}
class PostDaoImpl extends BaseDao implements IPostDao {
public async findByTitle(title: string) {}
}
class DaoFactory {
public static create(daoName: string): IBaseDao {
switch (daoName) {
case 'user':
return new UserDaoImpl();
case 'post':
return new PostDaoImpl();
default:
throw new Error(`no DAO for daoName = ${daoName}`);
}
}
}
const userDao: IUserDao = DaoFactory.create('user');
userDao.findByEmail();
But got this error:
Property 'findByEmail' is missing in type 'IBaseDao' but required in type 'IUserDao'.ts(2741)
Aucun commentaire:
Enregistrer un commentaire