Skip to content

Instantly share code, notes, and snippets.

@dewey92
Created October 9, 2018 22:38
Show Gist options
  • Select an option

  • Save dewey92/bdaf829f26fe8e6222c715c5be055e52 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/bdaf829f26fe8e6222c715c5be055e52 to your computer and use it in GitHub Desktop.
TS Generics dan enum
/* 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