-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ts):
Intersect
-> Prettify
; expand input types; add `& {…
…}`; docs This refactor brings this helper type in line with [the original inspiration](https://twitter.com/mattpocockuk/status/1622730173446557697). During #79 I encountered situations where the `& {}` was necessary to coax the compiler into untangling complex types. I also learned that the `Record`-based type constraint was unnecessary and that it has uses beyond just handling intersections, so I opted to fully implement the original helper except with a shorter name. I also took this opportunity to add a readme entry.
- Loading branch information
Showing
5 changed files
with
25 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
/** Represent an object type with all properties merged into the top level, | ||
* handling intersections (`&`). | ||
/** Represents a type with all properties merged into the top level, handling | ||
* intersections (`&`), often resulting in more legible shapes particularly | ||
* for object types. | ||
* | ||
* Source: https://twitter.com/mattpocockuk/status/1622730173446557697 | ||
* | ||
* @example | ||
* type USCity = { city: string, state: string }; | ||
* type USAddress = USCity & { street: string, zip: string }; | ||
* // ^? USCity & { street: string, zip: string } | ||
* | ||
* type USAddress = Intersect<USCity & { street: string, zip: string }>; | ||
* type USAddress = Pretty<USCity & { street: string, zip: string }>; | ||
* // ^? { city: string, state: string, street: string, zip: string } */ | ||
// deno-lint-ignore no-explicit-any | ||
export type Intersect<T extends Record<any, any>> = { [K in keyof T]: T[K] }; | ||
// deno-lint-ignore ban-types | ||
export type Pretty<T> = { [K in keyof T]: T[K] } & {}; |