Can't create optional thunk payload
See original GitHub issueThe Problem
I am building out a store using TypeScript. In it, I have a thunk which should accept an optional payload. However, I do not know how to properly define this in TypeScript.
This is the type of my thunk.
fetchStuff: Thunk<
IStuffModel,
{ language: Language; } | undefined,
void,
IStoreModel
>;
The Goal
The goal is to be able to call this thunk in two ways.
fetchStuff();
fetchStuff({ language: 'en' });
However, whenever I call it like fetchStuff() I receive the following type error.
Expected 1 arguments, but got 0.ts(2554)
index.d.ts(56, 4): An argument for 'payload' was not provided.
If I define the thunk payload type as {language: Language } | void I receive an error inside the thunk itself where I use payload?.language. The error says that language does not exist on type void. The same goes if I use null.
Not sure if this is a bug or if I am just missing something silly.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
action.payload in creactAsyncThunk is undefined
payload in undefined in the fulfilled action creator. Here is my code. /// Create Async Thunk export const fetchUserData = createAsyncThunk( ' ...
Read more >createAsyncThunk - Redux Toolkit
createAsyncThunk. Overview. A function that accepts a Redux action type string and a callback function that should return a promise.
Read more >Using Optional with Jackson - Baeldung
First, let's take a look at what happens when we try to serialize and deserialize Optionals with Jackson. 2.1. Maven Dependency.
Read more >remove (or add) optional field to JSON based on a value
I have a JSON object as my payload: ... I can't simply do: payload ... But I need the whole payload minus the...
Read more >TypeScript - Easy Peasy v5
If you wish to make your payload an optional value you can use a union. ... To define a thunk you need to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Ok, I refined this a bit more and have updated the docs. The payload will only be optional if you union with
undefined.If you union with
nullthen we will expect you to be passingnullor the alternative type into the payload. I think there are genuine cases where you may want to usenullas a signifying value.We can’t use
voidas it will cause some regressions with our capability to support generic models.The correct declaration for what you are trying to achieve is now:
This patch will be released shortly.
This still seems to be an issue when the type is not primitive. So for example: Given
We get a
Expected 1 arguments, but got 0. ts(2554)warning in the IDE (code still runs) when calling thisawait getActions.myThunk()Any ideas why this might be?