10LOC

#nominal-typing

TypeScriptintermediate

Branded types to stop mixing UserId and OrderId

type Brand<T, Name extends string> = T & { readonly __brand: Name };

export type UserId = Brand<string, "UserId">;
export type OrderId = Brand<string, "OrderId">;

Give two string IDs distinct types so passing one where the other belongs is a compile error, at zero runtime cost.