There is an official documentation, that I follow to create slice with TS -> RTK usage with TS, which describes how we can give type for our state.
But when you do so, there is a problem in my reducers, they expect my state to contain only status key, but not data.
What am I missing here? There is a demo, where you can see the problem.
This is a general problem regarding TypeScript union types, as your first option { status: 'loading' }
does not have a data
property. You can get rid of this particular error, Property 'data' does not exist on type
, by including data?: never
.
type SliceState = { state: 'loading'; data?: never } | { state: 'finished'; data: string }
But you will continue to have problems. Here's why:
Your union type says that the values of state
and data
must match. You cannot directly assign to either property because you risk creating an invalid state which does not meet the constraints of the union.
If the value of your state
variable is { state: 'loading' }
, then assigning state.data = action.payload
will create a new value of { state: 'loading'; data: 'some string' }
which is not assignable to SliceState
.
You have a few options here:
type SliceState = { state: 'loading' | 'finished'; data?: string };
as
assertion, for example:(state as any).data = action.payload;
dataReceived: (state, action) => {
return { state: 'finished, data: action.payload };
}