In my TS React App, I am using Zustand to manage the states of all dialogs/modals.
Here is how I typed the ModalStoreState:
type ModalStoreState = {
/**
* 1st arg is "name" of the target dialog, while 2nd arg is "data" corresponding with it.
*
* How should I type "data" in a way that when I create a getter fn, the type will be inferred correctly?
*/
opened: [string, any][];
};
While here is how I typed the ModalStoreActions:
type ModalStoreActions = {
open: (data: [string, any]) => void;
close: (name: string) => void;
/**
* Here is the getter fn, how do I type this one that it will infer the type of the object provided on open?
*/
getData: (name: string) => ...
};
export const useModalStore = create<ModalStore>((set, get) => ({
...initialState,
open: (data) => set((state) => ({ opened: state.opened.concat(data) })),
close: (name) => set((state) => ({ opened: state.opened.filter((x) => x[0] !== name) })),
getData: (name) => {
/**
* This is how I assumed the getter fn, would be implemented. :D
*/
const opened = get().opened.find((x) => x[0] === name);
if (!opened) {
return null;
}
return opened[1];
},
}));
Is it possible to infer the type of the "data" provided during "open" fn, considering that I should only be able to input either "object" or "null" type?
2
Answers
You can leverage TypeScript’s generics and conditional types.
Define a type that ensures the data can only be of type object or null:
Use generics to type ModalStoreState and ModalStoreActions.
Finally, apply this generic type when creating the Zustand store:
ModalData restricts the type of data to be either an object or null.
Generics (T) allow the getData to infer the correct type of data based on what was provided to open.
The return type of getData will be inferred correctly as either the type of data or null.
To ensure strong typing and type inference in Zustand for managing modals/dialogs, you can use TypeScript’s advanced types and conditional types. The goal is to make sure that the data parameter passed to the open function and retrieved by the getData function are correctly typed and consistent.
Here’s how you can achieve this:
1. Define Types for Modal Data
First, define a type that describes the data you expect for each modal. You can use a TypeScript interface or a type alias to specify what kind of data each modal should hold.
2. Define the Modal Store State
You need to ensure that opened in ModalStoreState can map modal names to their corresponding data types.
3. Define the Modal Store Actions
The open function needs to accept data that aligns with the modal name. You can use TypeScript generics to ensure type safety.
4. Implement the Modal Store
Use Zustand’s create function to implement the store and manage type safety:
An example of how you would use this store in a React component: