Skip to content

Instantly share code, notes, and snippets.

@piecyk
Forked from dcolthorp/flavoring-and-branding.ts
Created April 28, 2020 09:31
Show Gist options
  • Save piecyk/205a170a2d7fc200a6d95ce4ddf862ae to your computer and use it in GitHub Desktop.
Save piecyk/205a170a2d7fc200a6d95ce4ddf862ae to your computer and use it in GitHub Desktop.
/** Used by Flavor to mark a type in a readable way. */
export interface Flavoring<FlavorT> {
_type?: FlavorT;
}
/** Create a "flavored" version of a type. TypeScript will disallow mixing flavors, but will allow unflavored values of that type to be passed in where a flavored version is expected. This is a less restrictive form of branding. */
export type Flavor<T, FlavorT> = T & Flavoring<FlavorT>;
/** Used by Brand to mark a type in a readable way. */
export interface Branding<BrandT> {
_type: BrandT;
}
/** Create a "branded" version of a type. TypeScript won't allow implicit conversion to this type */
export type Brand<T, BrandT> = T & Branding<BrandT>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment