Skip to content

Commit

Permalink
docs: move section
Browse files Browse the repository at this point in the history
  • Loading branch information
castarco authored Jan 26, 2022
1 parent 7ccbc46 commit df9f528
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ const mail: Email = user // Error, as the flavors don't match
symbols instead of strings as property keys or property values when defining
the new nominal type.

### Faster brands and flavors

The types `WithBrand` and `WithFlavor`, although quite simple in their purpose,
hide a quite complex machinery that exists for the sole purpose of maintaining
full compatibility with other more complex types such as `WithProperty`.

Most times we won't really need to rely on such complex mechanisms because we
apply `WithBrandh` and `WithFlavor` to basic types. So, if we want to minimize
our compilation types, we can chose a simpler and faster implementation:

```typescript
import {
FastBrand,
FastFlavor,
WithBrand,
WithFlavor
} from '@coderspirit/nominal'

// These two types are 100% equivalent, but the second one takes less time to be
// compiled. Notice that they are 100% equivalent only because they were applied
// to "basic" types (without other associated metadata, like `WithProperty`).
type SlowEmailType = WithBrand<string, 'Email'>
type FastEmailType = FastBrand<string, 'Email'>

// Same for flavors.
type SlowPhoneNumberType = WithFlavor<string, 'PhoneNumber'>
type FastPhoneNumberType = FastFlavor<string, 'PhoneNumber'>
```
## Properties
Expand Down Expand Up @@ -151,35 +179,6 @@ type Wrong = WithStrictProperty<number, Parity, 'Seven'>
### Advanced use cases (pseudo dependent types)
#### Faster brands and flavors
The types `WithBrand` and `WithFlavor`, although quite simple in their purpose,
hide a quite complex machinery that exists for the sole purpose of maintaining
full compatibility with other more complex types such as `WithProperty`.
Most times we won't really need to rely on such complex mechanisms because we
apply `WithBrandh` and `WithFlavor` to basic types. So, if we want to minimize
our compilation types, we can chose a simpler and faster implementation:
```typescript
import {
FastBrand,
FastFlavor,
WithBrand,
WithFlavor
} from '@coderspirit/nominal'

// These two types are 100% equivalent, but the second one takes less time to be
// compiled. Notice that they are 100% equivalent only because they were applied
// to "basic" types (without other associated metadata, like `WithProperty`).
type SlowEmailType = WithBrand<string, 'Email'>
type FastEmailType = FastBrand<string, 'Email'>

// Same for flavors.
type SlowPhoneNumberType = WithFlavor<string, 'PhoneNumber'>
type FastPhoneNumberType = FastFlavor<string, 'PhoneNumber'>
```
#### **Properties can be preserved across function boundaries**
This feature can be very useful when we need to verify many properties for the
Expand Down

0 comments on commit df9f528

Please sign in to comment.