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
Copy file name to clipboardExpand all lines: book-content/chapters/16-the-utils-folder.md
+67-36Lines changed: 67 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -433,8 +433,6 @@ item.title;
433
433
434
434
In this case, the `assertIsAlbum` function doesn't check for the required properties of an `Album` - it just checks if `typeof input` is `"object"`. This means we've left ourselves open to a stray `null`. The famous JavaScript quirk where `typeof null === 'object'` will cause a runtime error when we try to access the `title` property.
435
435
436
-
<!-- CONTINUE -->
437
-
438
436
## Function Overloads
439
437
440
438
Function overloads provide a way to define multiple function signatures for a single function implementation. In other words, you can define different ways to call a function, each with its own set of parameters and return types. It's an interesting technique for creating a flexible API that can handle different use cases while maintaining type safety.
@@ -443,22 +441,26 @@ To demonstrate how function overloads work, we'll create a `searchMusic` functio
443
441
444
442
### Defining Overloads
445
443
446
-
To define function overloads, the same function definition is written multiple times with different parameter and return types. Each definition is called an overload signature, and is separated by semicolons.
444
+
To define function overloads, the same function definition is written multiple times with different parameter and return types. Each definition is called an overload signature, and is separated by semicolons. You'll also need to use the `function` keyword each time.
447
445
448
-
For the `searchMusic` example, we want to allow users to search by providing a string with keywords that will match songs, albums, or artists. We also want to allow for an advanced search by accepting an object with properties for `artist`, `genre`, and `year`.
446
+
For the `searchMusic` example, we want to allow users to search by providing an artist, genre and year. But for legacy reasons, we want them to be able to pass them as a single object or as separate arguments.
449
447
450
-
Here's how we could define these function overload signatures:
448
+
Here's how we could define these function overload signatures. The first signature takes in three separate arguments, while the second signature takes in a single object with the properties:
451
449
452
450
```typescript
453
-
function searchMusic(query:string):void;
451
+
function searchMusic(artist:string, genre:string, year:number):void;
454
452
function searchMusic(criteria: {
455
-
artist?:string;
456
-
genre?:string;
457
-
year?:number;
453
+
// red squiggly line under searchMusic
454
+
artist:string;
455
+
genre:string;
456
+
year:number;
458
457
}):void;
458
+
459
+
// Hovering over searchMusic shows:
460
+
// Function implementation is missing or not immediately following the declaration.
459
461
```
460
462
461
-
With the overloads in place, we can now define the implementation signature.
463
+
But we're getting an error. We've declared some ways this function should be declared, but we haven't provided the implementation yet.
462
464
463
465
### The Implementation Signature
464
466
@@ -467,53 +469,82 @@ The implementation signature is the actual function declaration that contains th
467
469
In this case, the implementation signature will take in a parameter called `queryOrCriteria` that can be either a `string` or an object with the specified properties. Inside the function, we'll check the type of `queryOrCriteria` and perform the appropriate search logic based on the provided arguments:
468
470
469
471
```typescript
470
-
function searchMusic(query:string):void;
472
+
function searchMusic(artist:string, genre:string, year:number):void;
// No overload expects 2 arguments, but overloads do exist that expect either 1 or 3 arguments.
514
523
```
515
524
516
-
While there aren't too many use cases for function overloads in typical application development, it can be useful for adding support for multiple arguments to your utilities and libraries. However, you'll want to use them sparingly since too many overloads can make a function difficult to understand and maintain. If you get to a point where a function has too many overloads, look into refactoring it into more focused functions or using other patterns like optional parameters or union types.
525
+
This error shows us that we're trying to call `searchMusic` with two arguments, but the overloads only expect one or three arguments.
526
+
527
+
### Function Overloads vs Unions
528
+
529
+
<!-- CONTINUE -->
530
+
531
+
Function overloads can be useful when you want to express multiple ways to call a function that spread out over different parameters. In the example above, we can either call the function with separate arguments or with a single object.
532
+
533
+
When you have the same number of arguments but different types, you can use a union type instead of function overloads. For example, if you want to allow the user to search by either artist name or criteria object, you could use a union type:
0 commit comments