type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

Question: is there any way to get the advantages of opaque and other options at the same time?

Open xenoterracide opened this issue 4 years ago • 0 comments

given

export type OrderPart = SetRequired<Except<Partial<OrderEntity>, 'id'>, 'orderId'>;
export type OrderOnly = Opaque<OrderPart>;

export class OrderAggregateFactory {
  constructor(private readonly em: EntityManager) {}

  order(part: OrderOnly): Promise<OrderEntity> {

if I use it like this... I get the benefits of both

      const orderPart: OrderPart = {
        status: OrderStatus.Open,
        orderId,
      };
      return new OrderAggregateFactory(em).order(orderPart as OrderOnly);

but if I write like the latter, I lose the benefits of SetRequired, and probably other things (this compies)

      return new OrderAggregateFactory(em).order({
        status: OrderStatus.Open,
      } as OrderOnly);

is their a way to write it "like" the latter example? but still have Required and other things cause compile errors?

xenoterracide avatar Jun 12 '20 02:06 xenoterracide