Modéliser votre
domaine métier
grâce aux types

@jgrenat@bsky.social

Slides : bit.ly/metier-types

Type ?

Boolean ?

True
False

Number ?

1
12347
2
0.5664908

Infinity

String ?

"1"
"JUG Summer Camp"
""

"Elm"

"笑う門 には 福来たる"

"🏖️"

Type = ensemble de valeurs possibles

Boolean

True
False

2 valeurs

Number = valeurs

String =  valeurs

Cardinalité

Domaine métier ?

L’ensemble des connaissances, règles, processus et concepts spécifiques liés à l’activité

Gestion des processus d'achats au sein de votre entreprise

  • PurchaseRequest
  • Product (SaaS ou objets physiques)
  • Negotiation
  • Customer
export type PurchaseRequest = {
  items: {
    product: Product;
    negotiation?: Negotiation;
  }[];
  customer: Customer;
  status: string;
  validationDate?: DateTime;
}
export type Product = {
  id: string;
  name: string;
  type: string;
  unitPrice: number;
  details: any;
}
export type Negotiation = {
  type: string;
  value: number;
}
export type Customer = {
  id: string;
  email: string;
  address: string;
}

Modélisation

Métier

2

2

Card(modelisation) == Card(business)

Primitive Obsession

export type Negotiation = {
  type: 'percentage' | 'fixed';
  value: number;
}
export type Negotiation = {
  isPercentage: boolean;
  value: number;
}

Encapsulation

Value Objects

Discriminated
Union

Types algébriques

type Color = "♣︎" | "♥︎" | "♦︎" | "♠︎";
type Rank = "A" | "K" | "Q"  // ...
type CardDetails = {
  isRed: boolean;
  isNumber: boolean;
}
type Card = {
  color: Color;
  rank: Rank;
}

Type Somme

Type Produit

Type Algébrique

2

2

4

52

4

13

13

4

Template Literal Type

export type Customer = {
  id: CustomerId;
  email: Email;
  address: string;
};
export type Negotiation = {
  type: "percentage" | "fixed" | "free";
  value: number;
};
export type Product = SaasProduct | PhysicalProduct;

type SaasProduct = {
  id: ProductId;
  name: string;
  type: "saas";
  unitPrice: number;
  details: { 
    saasId: SaasId, 
    subscriptionEnd: PlainDate, 
    billingQuarter: BillingQuarter 
  };
};

type PhysicalProduct = {
  id: ProductId;
  name: string;
  type: "physical";
  unitPrice: number;
  details: { itemId: ItemId; count: number };
};
export type PurchaseRequest = DraftPurchaseRequest 
  | SentPurchaseRequest | ValidatedPurchaseRequest;

type DraftPurchaseRequest = {
  items: PurchaseRequestItem[];
  customer?: {
    email: string;
    address: string;
  };
  status: "draft";
};

type SentPurchaseRequest = {
  items: NonEmptyArray<PurchaseRequestItem>;
  customer: Customer;
  status: "sent";
};

type ValidatedPurchaseRequest = {
  // ...
  validationDate: DateTime;
};
export type Currency = "USD" | "EUR";

export type Price<T extends Currency> = { 
  value: number; 
  currency: T 
};
  • Unions explicites
  • Value object
  • Types brandés
  • Types algébriques
  • Template literal types

Si c'est important pour votre métier, faites-en un type

Ajustez l'équilibre entre
pénibilité et sécurité 

Merci !

@jgrenat@bsky.social

Slides : bit.ly/metier-types

Liens

Slides : bit.ly/metier-types

@jgrenat@bsky.social

Slides : bit.ly/metier-types

@jgrenat@bsky.social