Created
October 9, 2018 22:38
-
-
Save dewey92/bdaf829f26fe8e6222c715c5be055e52 to your computer and use it in GitHub Desktop.
TS Generics dan enum
This file contains hidden or 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
| /* Enum */ | |
| enum Status { | |
| Initial, | |
| Fetching, | |
| Done, | |
| Error | |
| } | |
| interface ProductStore { | |
| products: ProductModel[]; | |
| status: Status; | |
| } | |
| const isFetchingProduct = (data: ProductStore) => | |
| product.status === Status.Fetching; | |
| /* Generics */ | |
| interface DataStore<T> { | |
| data: T; | |
| status: Status; | |
| } | |
| type ProductStore = DataStore<ProductModel[]>; | |
| type UserStore = DataStore<UserModel[]>; | |
| const isFetching = (dataStore: DataStore<any>) => | |
| dataStore.status === Status.IsFetching; | |
| const getData = <T>(dataStore: DataStore<T>) => dataStore.data; | |
| // typeof getData(productStore) == ProductModel[] | |
| // typeof getData(userStore) == UserModel[] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment