Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Toast]: Infer available toast types in showToast function #168

Closed
anuraghazra opened this issue Nov 24, 2020 · 1 comment
Closed

[Toast]: Infer available toast types in showToast function #168

anuraghazra opened this issue Nov 24, 2020 · 1 comment
Labels
enhancements 📈 Improve an existing Feature

Comments

@anuraghazra
Copy link
Contributor

anuraghazra commented Nov 24, 2020

image

https://stackoverflow.com/questions/51448291/how-to-create-a-generic-react-component-with-a-typed-context-provider

@anuraghazra anuraghazra added the enhancements 📈 Improve an existing Feature label Nov 24, 2020
@anuraghazra
Copy link
Contributor Author

anuraghazra commented Nov 25, 2020

After extensive experimentation and trying lot of things I concluded that getting the available toastTypes union is not possible in showToast function and here's why :-

On a surface level this looks pretty easy to do.
In <ToastProvider> we have the toastTypes prop and we pass available toasts in there like so

image

And it looks straight forward and we might do this just by infering the Union types from toastTypes with const assertion or just getting the keys with keyof.

But that actually does not work since the actual showToast methods definitions are already defined while creating the ContextProvider, in here:

interface ToastContextState extends ToastStateReturn {
  toastTypes: ToastTypes;
}

const ToastContext = React.createContext<ToastContextState | undefined>(
  undefined,
);

And thus it is not possible to get the union types from the toastTypes because it is a Record. so showToast method would always infer the {type} prop as "string" and there is no way to infer that.

export type ToastTypes = Record<
  string,
  React.FC<
    Pick<Toast, "content" | "id" | "isVisible"> & {
      hideToast: ToastStateReturn["hideToast"];
    }
  >
>;

I also tried using Generic functions too but that did not worked as well, because

  • ReturnType<> could not infer the types of a generic function, it will just return unknown as the type
  • And the ToastContextState is already defined so it would have no effect.

image

What i did is passed the Generic from ToastProvider -> useToastState -> showToast
If we just hardcode the union types in the useToastState it works as expected but would not work if ToastProvider passes it because the types of <ToastContextProvider value={{ ...state, toastTypes }}> are already defined.

Useful links and resources

Thus i'm closing this issue as wontfix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancements 📈 Improve an existing Feature
Projects
None yet
Development

No branches or pull requests

1 participant