瀏覽代碼

feat: `Prettify<T>` type

Sv443 5 月之前
父節點
當前提交
d7e8a31bce
共有 4 個文件被更改,包括 73 次插入3 次删除
  1. 5 0
      .changeset/fast-bananas-taste.md
  2. 1 0
      README-summary.md
  3. 59 3
      README.md
  4. 8 0
      lib/types.ts

+ 5 - 0
.changeset/fast-bananas-taste.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": minor
+---
+
+Added utility type `Prettify` to make complex types more readable

+ 1 - 0
README-summary.md

@@ -76,6 +76,7 @@ or view the documentation of previous major releases:
     - [`NonEmptyArray`](https://github.com/Sv443-Network/UserUtils#nonemptyarray) - any array that should have at least one item
     - [`NonEmptyString`](https://github.com/Sv443-Network/UserUtils#nonemptystring) - any string that should have at least one character
     - [`LooseUnion`](https://github.com/Sv443-Network/UserUtils#looseunion) - a union that gives autocomplete in the IDE but also allows any other value of the same type
+    - [`Prettify`](https://github.com/Sv443-Network/UserUtils#prettify) - expands a complex type into a more readable format while keeping functionality the same
 
 <br><br>
 

+ 59 - 3
README.md

@@ -78,6 +78,7 @@ View the documentation of previous major releases:
     - [`NonEmptyArray`](#nonemptyarray) - any array that should have at least one item
     - [`NonEmptyString`](#nonemptystring) - any string that should have at least one character
     - [`LooseUnion`](#looseunion) - a union that gives autocomplete in the IDE but also allows any other value of the same type
+    - [`Prettify`](#prettify) - expands a complex type into a more readable format while keeping functionality the same
 
 <br><br>
 
@@ -2429,7 +2430,7 @@ Usage:
 NonEmptyArray<TItem = unknown>
 ```
   
-This type describes an array that has at least one item.  
+This generic type describes an array that has at least one item.  
 Use the generic parameter to specify the type of the items in the array.  
   
 <details><summary><b>Example - click to view</b></summary>
@@ -2459,7 +2460,7 @@ Usage:
 NonEmptyString<TString extends string>
 ```
   
-This type describes a string that has at least one character.  
+This generic type describes a string that has at least one character.  
   
 <details><summary><b>Example - click to view</b></summary>
 
@@ -2483,7 +2484,7 @@ Usage:
 LooseUnion<TUnion extends string | number | object>
 ```
   
-A type that offers autocomplete in the IDE for the passed union but also allows any value of the same type to be passed.  
+A generic type that offers autocomplete in the IDE for the passed union but also allows any value of the same type to be passed.  
 Supports unions of strings, numbers and objects.  
   
 <details><summary><b>Example - click to view</b></summary>
@@ -2502,6 +2503,61 @@ foo(1);   // type error: Argument of type '1' is not assignable to parameter of
 ```
 </details>
 
+<br>
+
+## Prettify
+Usage:
+```ts
+Prettify<T>
+```
+  
+A generic type that makes TypeScript and your IDE display the type in a more readable way.  
+This is especially useful for types that reference other types or are very complex.  
+It will also make a variable show its type's structure instead of just the type name (see example).  
+  
+<details><summary><b>Example - click to view</b></summary>
+
+```ts
+// tooltip shows all constituent types, leaving you to figure it out yourself:
+// type Foo = {
+//   a: number;
+// } & Omit<{
+//   b: string;
+//   c: boolean;
+// }, "c">
+type Foo = {
+  a: number;
+} & Omit<{
+  b: string;
+  c: boolean;
+}, "c">;
+
+// tooltip shows just the type name:
+// const foo: Foo
+const foo: Foo = {
+  a: 1,
+  b: "2"
+};
+
+// tooltip shows the actual type structure:
+// type Bar = {
+//   a: number;
+//   b: string;
+// }
+type Bar = Prettify<Foo>;
+
+// tooltip again shows the actual type structure:
+// const bar: {
+//   a: number;
+//   b: string;
+// }
+const bar: Bar = {
+  a: 1,
+  b: "2"
+};
+```
+</details>
+
 <br><br><br><br>
 
 <!-- #region Footer -->

+ 8 - 0
lib/types.ts

@@ -31,3 +31,11 @@ export type LooseUnion<TUnion extends string | number | object> =
  * }
  */
 export type NonEmptyString<TString extends string> = TString extends "" ? never : TString;
+
+/**
+ * Makes the structure of a type more readable by expanding it.  
+ * This can be useful for debugging or for improving the readability of complex types.
+ */
+export type Prettify<T> = {
+  [K in keyof T]: T[K];
+} & {};