Skip to content

Instantly share code, notes, and snippets.

@jaandrle
Last active August 30, 2021 11:11
Show Gist options
  • Save jaandrle/be616470132afc15337708e09d20da07 to your computer and use it in GitHub Desktop.
Save jaandrle/be616470132afc15337708e09d20da07 to your computer and use it in GitHub Desktop.
TypeScript
// Brand<number, "USD">
type __BrandWitnes<B= unknown>= Partial<Record<'__witness__',B>>;
type Brand<Base= any, Branding= any, ReservedName extends PropertyKey= '__type__'>
= Base & Partial<Record<ReservedName,Branding>> & __BrandWitnes<Base>;
// BrandBase<Brand<number, "USD">>
type BrandBase<T extends __BrandWitnes>
= T['__witness__'];
// tuRecord<"a"|"b","A"|"B","a"> => { a?: "A"|"B", b: "A"|"B" }
type tuRecord<Keys extends PropertyKey, Values, KeysOptional extends PropertyKey= never>
= Required<Omit<Record<Keys,Values>,KeysOptional>>
& Partial<Record<KeysOptional, Values>>;
/* imperative
type tuRecord<Keys extends PropertyKey, Values, Optional extends PropertyKey= never>
= { [K in Exclude<Keys,Optional>]: Values } & { [J in Optional]?: Values };
*/
@jaandrle
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment