Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/denismurphy/emulating-associated-value-enums-typescript
Emulating Associated Value Enums in TypeScript
https://github.com/denismurphy/emulating-associated-value-enums-typescript
associated-values chai educational interface mocha npm swift types typescript union
Last synced: 2 months ago
JSON representation
Emulating Associated Value Enums in TypeScript
- Host: GitHub
- URL: https://github.com/denismurphy/emulating-associated-value-enums-typescript
- Owner: denismurphy
- License: mit
- Created: 2023-02-24T22:59:03.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-14T14:54:29.000Z (5 months ago)
- Last Synced: 2024-08-14T16:27:08.267Z (5 months ago)
- Topics: associated-values, chai, educational, interface, mocha, npm, swift, types, typescript, union
- Language: TypeScript
- Homepage:
- Size: 8.79 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ท๏ธ Emulating Associated Value Enums in TypeScript
This project demonstrates how to emulate Swift's Associated Value Enums in TypeScript.
๐ Swift allows defining Associated Value Enums that can contain other types within them. While TypeScript doesn't support this natively, it can be emulated using Interfaces and Type Unions.
## ๐ป Code Example
The code example creates a `Barcode` object with two methods:
- ๐ `upc`: Creates a new barcode object with UPC values
- ๐ฑ `qrCode`: Creates a new barcode object with QR code valuesIt also includes two utility methods:
- โ `isUPC`: Checks if a given barcode is a UPC
- โ `isQRCode`: Checks if a given barcode is a QR codeThe `switch` method returns a new `BarcodeSwitchBuilder` object that allows you to build a switch statement based on the barcode type.
## ๐งช Running the Tests
This project includes a Mocha test to ensure that the `BarcodeSwitchBuilder` works as expected.
To run the tests, execute the following command in your terminal:
```
npm test
```## ๐ Conclusion
Emulating Associated Value Enums in TypeScript can be achieved using Interfaces and Type Unions. The example provided in this project demonstrates one approach to accomplish this.