timolins / react-hot-toast

Smoking Hot React Notifications 🔥
https://react-hot-toast.com
MIT License
9.44k stars 309 forks source link

streamline ToastOption types with code logic #306

Open Tobi-mmt opened 10 months ago

Tobi-mmt commented 10 months ago

Problem

If I type the following code

toast.custom(„hello“,{type:“error“})

It does hand over the type to the custom handler. So I can access the type in my own component (which is very usefull to e.g. set the color for each type).

So this works well (except the TS error):

import { toast } from 'react-hot-toast/headless';

  ...

  return (
    <div className="fixed flex flex-col gap-3 top-12 right-0 z-50 p-4" onMouseEnter={startPause} onMouseLeave={endPause}>
      {toasts
        .filter((toast) => toast.visible)
        .map((toast) => (
          <div key={toast.id} {...toast.ariaProps}  >
            <p>type: {toast.type}</p> //<- I can see the type but typescript says: "type" is not defined
          </div>
        ))}
    </div>
  )
}

The reason for that is that the code is handing over everything: https://github.com/timolins/react-hot-toast/blob/1713dd3598ee5b746ccc9c66750d6f53394e58f1/src/core/toast.ts#L31

But the types does not allow handing over everything: https://github.com/timolins/react-hot-toast/blob/1713dd3598ee5b746ccc9c66750d6f53394e58f1/src/core/types.ts#L57-L69

Solution

We should add the type to the allowed ToastOptions.

devanfarrell commented 9 months ago

I personally would appreciate type being exposed as well. We use it as an argument all the time with ts-expect-error on it rather than use the helper functions because it makes the library much easier to test.