Repository navigation
[toast] Make useToastManager and createToastManager generic functions - #3882
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
Greptile OverviewGreptile SummaryThis PR makes Key Changes:
Backward Compatibility:
Documentation: Confidence Score: 5/5
Important Files Changed
|
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
useToastManager generic functionuseToastManager generic function
|
You should be able to make it backward‑compatible while still enabling the new typed manager by defaulting the method generic to the hook's export function useToastManager<Data extends object = any>(): UseToastManagerReturnValue<Data> {
...
export interface UseToastManagerReturnValue<Data extends object = any> {
toasts: ToastContext<Data>['toasts'];
add: <T extends object = Data>(options: ToastManagerAddOptions<T>) => string;
close: (toastId: string) => void;
update: <T extends object = Data>(toastId: string, options: ToastManagerUpdateOptions<T>) => void;
promise: <Value, T extends object = Data>(
promise: Promise<Value>,
options: ToastManagerPromiseOptions<Value, T>,
) => Promise<Value>;
}Spec file /* eslint-disable react-hooks/rules-of-hooks */
import { expectType } from '#test-utils';
import { useToastManager } from './useToastManager';
type ToastPayload = {
id: string;
count: number;
};
const typedManager = useToastManager<ToastPayload>();
const typedToastData = typedManager.toasts[0]?.data;
expectType<ToastPayload | undefined, typeof typedToastData>(typedToastData);
const typedAddId = typedManager.add({
title: 'typed',
data: {
id: 'typed',
count: 1,
},
});
expectType<string, typeof typedAddId>(typedAddId);
typedManager.update('typed', {
data: {
id: 'typed-update',
count: 2,
},
});
typedManager.promise(Promise.resolve(2), {
loading: 'loading',
success: (value) => ({
title: `${value}`,
data: {
id: 'typed-success',
count: value,
},
}),
error: 'error',
});
const legacyManager = useToastManager();
const legacyAddId = legacyManager.add<ToastPayload>({
title: 'legacy',
data: {
id: 'legacy',
count: 3,
},
});
expectType<string, typeof legacyAddId>(legacyAddId);
legacyManager.update<ToastPayload>('legacy', {
data: {
id: 'legacy-update',
count: 4,
},
});
legacyManager.promise<number, ToastPayload>(Promise.resolve(5), {
loading: 'loading',
success: (value) => ({
title: `${value}`,
data: {
id: 'legacy-success',
count: value,
},
}),
error: 'error',
}); |
|
@atomiks nice, just updated the PR with your suggested changes. Did you want me to add that spec as well, or were you just sharing to demonstrate that it works as expected? If you'd like me to add the spec, let me know. |
|
@solastley yeah, should add it |
|
@atomiks hm, might be missing something, but I don't think these tests work since |
|
The spec file is only for types, it doesn't test runtime behavior. We have other spec files (filename.spec.tsx) for this purpose. |
|
@atomiks done |
|
@solastley thanks! I think this also applies to |
|
@atomiks yeah good call. Modified |
| add: <Data extends object>(options: ToastManagerAddOptions<Data>) => string; | ||
| export interface UseToastManagerReturnValue<Data extends object = any> { | ||
| toasts: ToastContext<Data>['toasts']; | ||
| add: <T extends object = Data>(options: ToastManagerAddOptions<T>) => string; |
There was a problem hiding this comment.
These need to be T extends Data = Data to catch wrong shaped payloads:
typedManager.add({
title: 'wrong-shape',
data: {
id: 'test',
// @ts-expect-error - message is not a valid property
message: 'not a number',
},
});
typedManager.add({
title: 'wrong-shape',
// @ts-expect-error - count is a missing property
data: {
id: 'test',
},
});There was a problem hiding this comment.
Can you add those calls I made to the spec file?
atomiks
left a comment
There was a problem hiding this comment.
Thanks for the contribution @solastley!
useToastManager generic functionuseToastManager and createToastManager generic functions
Overview
This PR makes the hook
useToastManagera generic function so that the returnedtoasts,add,update, andpromisefunctions all inherit the type argument provided touseToastManager.I thought this might be worth a discussion. For my use case (and I imagine this is true for most folks, but perhaps not), I have a single type which represents a discriminated union of all possible types that can be used as the type of
datain toast objects. When I access the toast manager withuseToastManager, it would be very convenient to specify this type at that time and not need to type cast when using thetoastson the returned toast manager.Example
For example, today I need to do something like the following:
With this change, if the type
CustomToastwere provided touseToastManageras a type argument in the above example, thentoast.datawould be automatically typed asCustomToast.Concerns
This change modifies
add,update, andpromiseto no longer be generic. So I believe this would constitute a breaking change for those methods if consumers are currently providing type arguments to them.