Last active
August 30, 2021 11:11
-
-
Save jaandrle/be616470132afc15337708e09d20da07 to your computer and use it in GitHub Desktop.
TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/piotrwitek/utility-types