question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't create optional thunk payload

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ctrlplusbcommented, Oct 15, 2020

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 null then we will expect you to be passing null or the alternative type into the payload. I think there are genuine cases where you may want to use null as a signifying value.

We can’t use void as it will cause some regressions with our capability to support generic models.

The correct declaration for what you are trying to achieve is now:

Thunk<
  StoreModel,
  string | undefined
>;

This patch will be released shortly.

0reactions
DanielPe05commented, May 13, 2022

This still seems to be an issue when the type is not primitive. So for example: Given

... Model
interface Payload {
  a?: string
  b?: number
}
myThunk: Thunk<MayModel, Payload | undefined, unknown, StoreModel>

... Implementation
myThunk: thunk((actions, payload = {}, { getStoreActions }) => {
    if (payload?.a) {
       // do something
    }

    actions.doSomethingElse(payload?.b || 1)
  }),

We get a Expected 1 arguments, but got 0. ts(2554) warning in the IDE (code still runs) when calling this await getActions.myThunk()

Any ideas why this might be?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found