Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jdegand/function-overload
Angular Challenges #15 Overload Function
https://github.com/jdegand/function-overload
angular angular-challenges typescript
Last synced: about 1 month ago
JSON representation
Angular Challenges #15 Overload Function
- Host: GitHub
- URL: https://github.com/jdegand/function-overload
- Owner: jdegand
- Created: 2023-09-02T20:14:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-02T20:15:59.000Z (over 1 year ago)
- Last Synced: 2024-04-16T09:08:21.322Z (8 months ago)
- Topics: angular, angular-challenges, typescript
- Language: TypeScript
- Homepage:
- Size: 109 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Function Overload
[Angular Challenges](https://github.com/tomalaforge/angular-challenges) #15 Overload Function solution.
## Directions
### Information
Angular uses TypeScript, and mastering TypeScript can help you avoid runtime errors by catching them at compile time.
In this challenge, we have a function to create a vehicle. However, each vehicle type requires different mandatory properties.
Currently, we are getting an error at runtime if one property is missing and we don't get the return Type, which is not ideal.
One solution would be to create a separate function for each vehicle type, but for this challenge, I want to use the same function and have TypeScript automatically complete the properties depending on the type passed as the first parameter.To achieve this, we will use overload functions.
### Statement
- Use function overload
## Built With
- [Angular](https://angular.io)
- [Angular CLI](https://github.com/angular/angular-cli) version 16.2.0.## Thoughts
- `isPublicTransport property is missing for type bus` - Initial error when first running the app.
- Definitely, a weird challenge where you still end up with an error being thrown. No way to make the bus variable actually return a Bus object.
- The coder mixed up the bus and boat signatures.
- Now, at least you get the error before runtime.
- First thought was to copy the function signature and change return type to all the different kinds of vehicles.
- Then I played with the parameters until I ran into errors with required and optional parameters.
- You can't have required parameters come after an optional parameter.
- I used undefined to get around this on the Bus & Boat signatures.
- Thomas' solution combined fuel & capacity. I took the approach where no other code besides signatures was to be changed.
- I originally thought the challenge solution would have no errors and the code would compile.
- Most general function signature has to be last.## Useful Resources
- [Medium](https://medium.com/ngconf/function-overloading-in-typescript-8236706b2c05) - Thomas' function overloading article
- [Typescript Lang](https://www.typescriptlang.org/docs/handbook/2/functions.html) - functions
- [Tutorials Teacher](https://www.tutorialsteacher.com/typescript/function-overloading) - function overloading
- [Stack Overflow](https://stackoverflow.com/questions/39407311/typescript-class-overload-signature-is-not-compatible-with-function-implementa) - typescript class overload signature is not compatible with function implementation