Redux-toolkit: createAction with required payload

Created on 6 Dec 2020  路  4Comments  路  Source: reduxjs/redux-toolkit

Hi! Can you provide example how to create action with required payload?
for example if i create like this

const foo = createAction<{ email: string }>('some_type');

type of foo() equals to ActionCreatorWithOptionalPayload
How to get it with ActionCreatorWithPayload?

Most helpful comment

@phryneas thanks much. After couple of fixes everything works as expected.

"strict": true,
"strictNullChecks": true,

was helpful

All 4 comments

I'm 99% sure you're using TypeScript with strictNullChecks: false in your tsconfig.json.

That means that your code up there is handled by TypeScript like const foo = createAction<{ email: string } | null | undefined>('some_type'); - and that undefined is interpreted by RTK as "this is optional". (every type definition you do will always be | null | undefined internally if you have strictNullChecks: false)

Other than you enabling strict mode for your TypeScript config, there is unfortunately not much we can do about it.

@phryneas thanks for reply but unfortunately strictNullChecks: true not helped.

const foo = createAction<any>('some_type') with that i got what i need becouse of IsAny<P, ActionCreatorWithPayload<any, T> but any...

image

Could you provide a reproduction for that? From my side, everything works as expected as you can see in this playground

@phryneas thanks much. After couple of fixes everything works as expected.

"strict": true,
"strictNullChecks": true,

was helpful

Was this page helpful?
0 / 5 - 0 ratings