Created
December 8, 2021 20:23
-
-
Save xxKRASHxx/c76eb3b444cc5dda826011a6a4c7437e to your computer and use it in GitHub Desktop.
Union types in Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { } from 'firebase-admin' | |
import Hub from '../../../../../documents/hub' | |
import Item from '../../../../../documents/item' | |
type AnalyticsInput = | |
| UserAnalyticsInput | |
| HubAnalyticsInput | |
| ItemAnalyticsInput | |
enum AnalyticsInputType { | |
user, hub, item | |
} | |
interface UserAnalyticsInput { | |
type: AnalyticsInputType.user | |
userId: String | |
} | |
interface HubAnalyticsInput { | |
type: AnalyticsInputType.hub | |
hubId: String | |
} | |
interface ItemAnalyticsInput { | |
type: AnalyticsInputType.item | |
itemId: String | |
} | |
export default { | |
User: { | |
analytics(id: String): UserAnalyticsInput { | |
return { | |
type: AnalyticsInputType.user, | |
userId: id | |
} | |
} | |
}, | |
Hub: { | |
analytics(ref: FirebaseFirestore.DocumentReference<Hub>): HubAnalyticsInput { | |
return { | |
type: AnalyticsInputType.hub, | |
hubId: ref.id | |
} | |
} | |
}, | |
Item: { | |
analytics(ref: FirebaseFirestore.DocumentReference<Item>): ItemAnalyticsInput { | |
return { | |
type: AnalyticsInputType.item, | |
itemId: ref.id | |
} | |
} | |
}, | |
Analytics: { | |
daily(input: AnalyticsInput) { | |
switch (input.type) { | |
case AnalyticsInputType.user: | |
input.userId; | |
break; | |
case AnalyticsInputType.hub: | |
input.hubId; | |
break; | |
case AnalyticsInputType.item: | |
input.itemId; | |
break; | |
} | |
return [] | |
}, | |
retention(input: AnalyticsInput) { | |
switch (input.type) { | |
case AnalyticsInputType.user: | |
input.userId; | |
break; | |
case AnalyticsInputType.hub: | |
input.hubId; | |
break; | |
case AnalyticsInputType.item: | |
input.itemId; | |
break; | |
} | |
return [] | |
}, | |
total(input: AnalyticsInput) { | |
switch (input.type) { | |
case AnalyticsInputType.user: | |
input.userId; | |
break; | |
case AnalyticsInputType.hub: | |
input.hubId; | |
break; | |
case AnalyticsInputType.item: | |
input.itemId; | |
break; | |
} | |
return [] | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment