I have the following create function, which have two definitions:
// Definition 1
export async function create<Entity>(
entity: ObjectType<Entity>,
data: DeepPartial<Entity>
): Promise<Entity>;
// Definition 2
export async function create<Entity>(
entity: ObjectType<Entity>,
data: DeepPartial<Entity>,
pickValues: keyof Entity
): Promise<Partial<Entity>>;
// Implementation
export async function create<Entity>(
entity: ObjectType<Entity>,
data: DeepPartial<Entity>,
pickValues?: keyof Entity
): Promise<Entity | Partial<Entity>> {
const { manager } = await connectDatabase();
const instance = manager.create(entity, data);
const createdEntity = await manager.save(instance);
return typeof pickValues === 'undefined'
? createdEntity
: pick(createdEntity, pickValues);
}
Is there a way to merge those two definitions in the typings of the function implementation? Maybe with a conditional return type... Something like this:
A extends B ? c : d;