How to Create a Dispatch Function that is Strongly Typed with its Payload?<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to Create a Dispatch Function that is Strongly Typed with its Payload?

Posted on: April 12, 2023


You want to send an action from a list of specific possible actions. The action (called in this article trigger) must be accompanied of data that need to be strongly typed. Thus, each trigger must have a unique name and a payload with a type determined when the developer writes down the list of possible triggers.

Then, you have a function that dispatches one of these triggers into your system. Depending on the trigger name, you want that function only to accept the correct payload. How would you accomplish the association? Furthermore, if you associate the action with a specific property of one of your business model objects, how can you specify the field without the user having a typo? For example, how can you be sure that if you determine a trigger "Action1" that accepts "number" the consumer of the developer who calls the dispatch of the trigger only dispatches "Action1" with a number in its payload?

First Try: Strongly Typed Object that Holds a Strongly Typed List of Trigger

Before digging into the details, let's establish some testing types we can associate with different triggers.

1interface FakeObj1 {
2 firstname: string;
3 lastname: string;
5interface FakeObj2 {
6 age: number;

Also, let's define two different kinds of triggers. That might be handy to dispatch an action depending on specific user interaction.

1type TriggerKind = "onClick" | "onTouch";

The next part is to associate a list of triggers to an object that will be our source of acceptable dispatching action.

For example, we can have the trigger onClick to accept a specific property of FakeObj1 or FakeObj2 while onTouch only accepts a single property of the FakeObj1.

1interface Triggers {
2 allowedTriggers: Trigger[];
5const triggers: Triggers = {
6 allowedTriggers: [
7 {
8 triggerKind: "onClick",
9 triggerInfo: {
10 fieldName: "firstname",
11 value: "defaultValue1",
12 },
13 },
14 {
15 triggerKind: "onClick",
16 triggerInfo: {
17 fieldName: "lastname",
18 value: "defaultValue2",
19 },
20 },
21 {
22 triggerKind: "onTouch",
23 triggerInfo: {
24 fieldName: "age",
25 value: 3,
26 },
27 },
28 ],

We will define Trigger soon, but the idea is to have a property triggerKind that accepts only triggerKind and triggerInfo that indicate which field of the object we want to from a specific object. What is missing is to type the fieldName strongly. Also, we notice that a value is specified. From that field, we could later determine using typeof the actual type of the field.

1interface TriggerInfo<T, Y> {
2 fieldName: keyof T;
3 value: Y;
6interface Trigger<T = any, Y = any> {
7 triggerKind: TriggerKind;
8 triggerInfo: TriggerInfo<T, Y>;
11interface Triggers {
12 allowedTriggers: Trigger[];

Then, we need to modify the initialization of the triggers variable.

1const triggers: Triggers = {
2 allowedTriggers: [
3 {
4 triggerKind: "onClick",
5 triggerInfo: {
6 fieldName: "firstname",
7 value: "defaultValue1",
8 },
9 } satisfies Trigger<FakeObj1, string>,
10 {
11 triggerKind: "onClick",
12 triggerInfo: {
13 fieldName: "lastname",
14 value: "defaultValue2",
15 },
16 } satisfies Trigger<FakeObj1, string>,
17 {
18 triggerKind: "onTouch",
19 triggerInfo: {
20 fieldName: "age",
21 value: 3,
22 },
23 } satisfies Trigger<FakeObj2, number>,
24 ],

A dispatch function is needed to test how we interact with these determined triggers. Because we do not care about implementing the function, we will only define the type using declare.

1declare function dispatch<T, Y>(triggerKind: Trigger<T, Y>, value: Y): void;

We can try using:

1dispatch<FakeObj1, string>(
2 {
3 triggerKind: "onClick",
4 triggerInfo: {
5 fieldName: "firstname",
6 value: "MyFirstNameHere",
7 },
8 },
9 222
10); // Does not compile, expect a string: That is what we want
12dispatch<FakeObj2, string>(
13 {
14 // Notice the string here
15 triggerKind: "onClick",
16 triggerInfo: {
17 fieldName: "age",
18 value: "MyFirstNameHere", // Should be number
19 },
20 },
21 "test"

The problem is the value inside the property triggerInfo on the second example passes the compilation when it should fail. The reason is that the second type passed in the generic type is string. Changing to number fix the problem. However, we must revise the design because the user is forced to redefine the value while the trigger list already contains that information. We want the user of the dispatch to rely on the definition set prior to the dispatch.

Second try: Literal for Trigger Name

To extract the exact trigger type, having a unique name is the best way. From a union of types, if we have a unique field that has a literal type, we can find out the remaining property of the object.

1interface TriggerInfo<T = any, Y = any> {
2 triggerName: `${string & keyof T}_${TriggerKind}`;
3 value: Y;
5interface Trigger<T = any, Y = any> {
6 triggerInfo: TriggerInfo<T, Y>;
8const triggers: Triggers = {
9 allowedTriggers: [
10 {
11 triggerName: "firstname_onClick",
12 value: "defaultValue1",
13 } satisfies TriggerInfo<FakeObj1, string>,
14 {
15 triggerName: "lastname_onClick",
16 value: "defaultValue2",
17 } satisfies TriggerInfo<FakeObj1, string>,
18 {
19 triggerName: "age_onTouch",
20 value: 3,
21 } satisfies TriggerInfo<FakeObj2, number>,
22 ],

The code is shorter since we no longer defined a triggerKind. Instead, it is combined in the triggerName that is the unique key. The additional benefit is the side-effect that we will be sure only to define a single trigger for each field. However, there is still a problem when calling the dispatch function.

1declare function dispatch<T, Y>(triggerKind: Trigger<T, Y>, value: Y): void;
3dispatch<FakeObj1, string>(
4 {
5 triggerInfo: {
6 triggerName: "firstname_onClick",
7 value: "MyFirstNameHere",
8 },
9 },
10 "test"
13dispatch<FakeObj2, string>(
14 {
15 triggerInfo: {
16 triggerName: "age_onTouch",
17 value: "MyFirstNameHere", // Should be number
18 },
19 },
20 "test"

The problem is the same as before. The dispatch requires an explicit mention of the value type.

Third try: Removing the Type of Dispatch

What we want is to only specify the trigger we want to "trig" in the dispatch. Thus, a definition that is like the following:

1declare function dispatch<T extends TriggerInfo>(
2 triggerKind: TriggerAction<T>
3): void;

Instead of passing a Trigger, we are passing a new type called TriggerAction. The goal is to pass a value with the string literal that uniquely identifies the trigger.

1interface TriggerAction<T extends TriggerInfo> {
2 triggerName: T["triggerName"];
3 value: any;

Now the execution looks like this:

2 triggerName: "firstname_onClick",
3 value: "test",
5dispatch<TriggerInfo<FakeObj2>>({ triggerName: "age_onTouch", value: "test" });

It still does not work correctly with the value because we accept any. However, having a typo in the triggername works well, and the code does not compile.

Forth try: Any to Extract Type

The next step is to extract the type instead of any.

1interface TriggerAction<T extends UniqueKeyTrigger> {
2 triggerName: T;
3 value: Extract<AllTriggersUnionTypes, { triggerName: T }>["_valueType"];

The value is of type Extract, a TypeScript's utility type that extracts the type that fulfills the requirement specified in the after the comma. Hence, we need a first type (called AllTriggersUnionTypes in this example) and a second type { triggerName: T }.

First, we need to define the type that will contain all the valid triggers. We already have a list of triggers but now we must extract their type:

1type Unpacked<T> = T extends (infer U)[] ? U : T;
2type AllTriggersUnionTypes = Unpacked<typeof triggers>;

The Unpacked takes the type of the array. So, if you have number[] it would return the type number. The typeof determines the type of the triggers list. It is important to not explicitly type the list with Trigger[]. We want each element to be not a Trigger but to be unique using the field triggerName as a string literal type. However, we can still have some type safety by using satisfies to ensure the anonymous type has the expected shape.

1const triggers = [
2 // Notice the lack of explicit type here; more later in this article about it
3 {
4 triggerName: "FakeObj1.firstname_onClick",
5 valueType: "string",
6 _valueType: "",
7 } satisfies TriggerInfo<FakeObj1, string>,
8 {
9 triggerName: "FakeObj1.lastname_onClick",
10 valueType: "string",
11 _valueType: "",
12 } satisfies TriggerInfo<FakeObj1, string>,
13 {
14 triggerName: "FakeObj2.age_onTouch",
15 valueType: "number",
16 _valueType: 0,
17 } satisfies TriggerInfo<FakeObj2, number>,

Some modifications in the TriggerInfo as you see. The trigger name has a specific format, and we have valueType and _valueType. One is for runtime, and one if for TypeScript to know the type. For example, we can use at runtime the first trigger to determine that the type is a "string" and at design type to use type _valueType to know that the type is string.

It is possible to define the format of the string literal, to ensure that we have a standard format.

1interface TriggerInfo<T = any, Y = any> {
2 triggerName: `${string & T}.${string & keyof T}_${TriggerKind}`;
3 valueType: TriggerValueType;
4 _valueType: Y;

At this point, we call dispatch with the trigger name and value:

1declare function dispatch<T extends UniqueKeyTrigger>(
2 triggerKind: TriggerAction<T>
3): void;

The triggerKind is changed to extract the _valueType.

1interface TriggerAction<T extends UniqueKeyTrigger> {
2 triggerName: T;
3 value: Extract<AllTriggersUnionTypes, { triggerName: T }>["_valueType"];

Because T is shared between the triggerName, TypeScript can, with the value that uses Extract determine that the one is from the union of type that has the triggerName specified.

1dispatch({ triggerName: "FakeObj1.firstname_onClick", value: "test" });
2dispatch({ triggerName: "FakeObj2.age_onTouch", value: 2 });
3dispatch({ triggerName: "FakeObj1.firstname_onClick", value: "false" });

The dispatch is strongly typed. An unknown name fails. Having the wrong kind for a specific field fails. Passing the incorrect value type fail.


The final example remains imperfect, with the major flaw that the triggers list is implicitly typed. At first, it might not sound like an issue since there is still some structural validation. However, if you need to store this list in another object, how would you do?

1interface MySystem {
2 triggers: ????;

Well, you can always extract the type explicitly yourself using typeof:

1interface MySystem {
2 systemTriggers: typeof triggers;

Then, you can set the list of triggers:

1const syst: MySystem = {
2 systemTriggers: triggers,

However, you can hardly directly set the triggers without this triggers object since it would mean that the MySystem would not have a type to specify in its interface.

The current design can scale to have many lists of triggers. That is important if your system is defining these triggers across your application.

1const triggers1 = [
2 {
3 triggerName: "FakeObj1.firstname_onClick",
4 valueType: "string",
5 _valueType: "",
6 } satisfies TriggerInfo<FakeObj1, string>,
7 {
8 triggerName: "FakeObj1.lastname_onClick",
9 valueType: "string",
10 _valueType: "",
11 } satisfies TriggerInfo<FakeObj1, string>,
14const triggers2 = [
15 {
16 triggerName: "FakeObj2.age_onTouch",
17 valueType: "number",
18 _valueType: 0,
19 } satisfies TriggerInfo<FakeObj2, number>,
21type AllTriggersUnionTypes =
22 | Unpacked<typeof triggers1>
23 | Unpacked<typeof triggers2>;
24// Snip
25interface MySystem {
26 systemTriggers: AllTriggersUnionTypes[];
29const syst: MySystem = {
30 systemTriggers: [...triggers1, ...triggers2],

Finally, a flaw is that the dispatch function is strongly bounded to UniqueKeyTrigger linked directly to AllTriggersUnionTypes. It is not a problem for internal use, but if you are building a third-party library, let your user specify the type differently. For example, on the dispatch, select a set (or subset) of triggers to check the validity. Consequently, modifying to have the trigger type only on dispatch might limit future use of the trigger. So far, the dispatch does nothing, but in a real system, the trigger might have another part of the system to react and would need to get the payload. Ideally, the consumer of the trigger should have the message strongly typed. Fortunately, because of the uniqueness of the triggerName it would be possible if the consumer has access to the trigger list to extract the type, like the dispatch function. But still, it remains that the list of triggers takes a central role in keeping the type traveling the stack of code.


The current article shows the evolution of a simple task into a better-typed model. Dispatching an action name with a value that has a pre-determined type occurs in many scenarios for data model framework or if you have a dynamic system for your user to connect different typed systems.

The simple solution proposed relies on a string literal for a type extraction of a list of potential actions that the developer can choose. Furthermore, the design allows the extraction of pieces of information from the triggerName for the dispatch consumer to use if needed. For example, the user could extract the first part to get the interface's name, the middle part to know the property of the interface, or the last part to see the action performed.