-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
exportable case reducers type, bugfix, documentation #290
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit 96b26c4 https://deploy-preview-290--redux-starter-kit-docs.netlify.com |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 96b26c4:
|
src/createSlice.ts
Outdated
} | ||
/** | ||
* The type describing a slice's `reducers` option. | ||
* Also checks itself, so it has to be passed "itself" as it's second option. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wat :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wouldn't have found out about that one myself as it is completely counter-intuitive. But a generic can extend
(which essentially just means "match this shape") something that depends on the generic being matched.
So you can write
function something<X extends Restrict<X>>(x: X) {}
and then Restrict
can be a type that does some crazy restrictions on the original input object while referencing it.
This is something I learned from @jcalz over on StackOverflow: https://stackoverflow.com/questions/59200095/is-there-a-simpler-way-of-associating-a-key-to-a-value-in-a-mapped-obect?noredirect=1#comment104621839_59200095
Thank you again Joe!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still waiting for that blog post / talk on "all the bizarre stuff I learned working on RTK's TS types" :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, got something in the making :) Meetup somewhere mid-february, maybe I'll also submit it to a few conferences. Also, some ideas to experiment with twitter-content :)
Your ability to crank out TS improvements continues to amaze me :) No specific feedback from me on the changes, but as usual I still don't know quite enough to provide much feedback here anyway. Do you want to try to get the other type doc changes in here too, or save those for later? |
This project is a wonderful learning experience, but it kills time like nothing else. I think i've started this Refactor over four or five times, each time putting in like 3-5 hours and then throwing everything away because it just wasn't working out.
I'm planning on doing everything in this PR, now that I already mixed different things in here. |
So it seems that relying on The alternative would be to move the type to a different file where nothing is exported from, but right now I'd say that the type is in the file it belongs to. |
Yeah, if that's necessary to keep internal types from leaking, we can do that. |
Okay, that's it for today. All that's left is fixing TS3.3 - I hope that's gonna work out, but I'll leave that problem for "future me" 😄 |
So, to make this work with TS <3.4, unfortunately I had to move But all in all, given that |
{ | ||
interface GenericState<T> { | ||
data?: T | ||
status: 'loading' | 'finished' | 'error' | ||
} | ||
|
||
const createGenericSlice = < | ||
T, | ||
Reducers extends SliceCaseReducers<GenericState<T>> | ||
>({ | ||
name = '', | ||
initialState, | ||
reducers | ||
}: { | ||
name: string | ||
initialState: GenericState<T> | ||
reducers: Reducers & ValidateSliceCaseReducers<GenericState<T>, Reducers> | ||
}) => { | ||
return createSlice({ | ||
name, | ||
initialState, | ||
reducers: { | ||
start(state) { | ||
state.status = 'loading' | ||
}, | ||
success(state: GenericState<T>, action: PayloadAction<T>) { | ||
state.data = action.payload | ||
state.status = 'finished' | ||
}, | ||
...reducers | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what wrapping createSlice
would look like now.
Oh, seriously? Now TS is acting up, only in 3.4 🙄 Just for the "extend slice" test case.
Might be on to something... |
ae8d90f
to
fbd29f0
Compare
Yay, we're all green, without dirty hacks. I was almost rage-quitting there o_O |
No immediate complaints about the TS stuff, but my eyes also mostly glazed over, so take it as you will :) Saw a couple possible comment typos, suggested a rewording, and it'd be nice to see the "generic slice" example in the docs, but other than that looks okay. |
Co-Authored-By: Mark Erikson <mark@isquaredsoftware.com>
Added a "Wrapping |
Awright, let's get this in. |
This is what I'm currently working on and it has already gotten a bit bigger than expected 😄
INCLUDED:
createSlice
and add new types (exported) to be used in situations like Export Types #276 and Creating Generic Slices #286 :SliceCaseReducerDefinitions
CaseReducerActions
CaseReducerWithPrepare
(not really necessary to export this, butCaseReducer
was already exported so it might not hurt to have this)PayloadActionCreator<any>
becomesActionCreatorWithoutPayload
which was a regression introduced in * refactor actionCreator typings fromtype
tointerface
#273 (also opened fix IsAny for the case when False is a superset of True joonhocho/tsdef#4 )_ActionCreatorWithPreparedPayload
is now marked@internal
(wasActionCreatorWithPreparedPayload
before * refactor actionCreator typings fromtype
tointerface
#273, since we renamed it anyway might as well hide it completely)@internal
types will be stripped from thedist/typings.d.ts
filecreateSlice
api-extractor trims too agressively (complete removal instead of "not export") - I'll have to see if that works out. (see [api-extractor] just "don't export" @internal types instead of stripping them microsoft/rushstack#1664 )(solved by manually exporting)