You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Majority of function arguments should be required (use optional sparingly). [⭣](#required--optional-args)
60
60
- Avoid type assertions. [⭣](#type--non-nullability-assertions)
61
61
- Strive for functions to be pure, stateless and have single responsibility. [⭣](#functions)
62
62
- Strong emphasis to keep naming conventions consistent and readable. [⭣](#naming-conventions)
@@ -182,7 +182,7 @@ type GuestUser = {
182
182
temporaryToken:string;
183
183
};
184
184
185
-
//Discriminating union type 'User' with no optional properties
185
+
//Discriminated union type 'User' with no optional properties
186
186
typeUser=AdminUser|RegularUser|GuestUser;
187
187
```
188
188
@@ -195,7 +195,7 @@ You may encounter discriminated unions under different names such as tagged unio
195
195
196
196
Discriminated unions advantages:
197
197
198
-
- As mentioned in [Args as Discriminated Union](#args-as-discriminated-union), discriminated union eliminates optional object properties which decreases complexity on function API.
198
+
- As mentioned in [Required & Optional Object Properties](#required--optional-object-properties), [Args as Discriminated Union](#args-as-discriminated-type) and [Props as Discriminated Type](#props-as-discriminated-type), discriminated union eliminates optional object properties which decreases complexity.
199
199
- Exhaustiveness check - TypeScript can ensure that all possible variants of a type are implemented, eliminating the risk of undefined or unexpected behavior at runtime. ([eslint rule](https://typescript-eslint.io/rules/switch-exhaustiveness-check/))
200
200
201
201
```ts
@@ -442,7 +442,7 @@ transformUserInput({
442
442
If the function becomes too complex, it probably should be broken into smaller pieces.
443
443
An exaggerated example where implementing 10 functions with 5 required args each, is better then implementing one "can do it all" function that accepts 50 optional args.
444
444
445
-
### Args as Discriminated Union
445
+
### Args as Discriminated Type
446
446
447
447
When applicable use **discriminated union type** to eliminate optional properties, which will decrease complexity on function API and only required properties will be passed depending on its use case.
448
448
@@ -473,6 +473,7 @@ type StatusErrorParams = {
473
473
error: string;
474
474
};
475
475
476
+
// Discriminated function param 'StatusParams' with no optional properties
0 commit comments