Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.2k
explicitly castinitialState in docs examples#827
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Deploy preview forredux-starter-kit-docs ready! Built with commit2a22180 https://deploy-preview-827--redux-starter-kit-docs.netlify.app |
This pull request is automatically built and testable inCodeSandbox. To see build info of the built libraries, clickhere or the icon next to each commit SHA. Latest deployment of this branch, based on commit2a22180:
|
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.
LGTM. I'm also doing this same thing in the rtk-query examples already 👍
completely forgot to merge this one 🤦 |
polaroidkidd commentedJan 4, 2021
quick question: does this mean that if I declare my state as suggested I'll have to declare every possible state permutation? |
@polaroidkidd only if you want your state to accept only very specific shapes. |
polaroidkidd commentedJan 7, 2021
I'm aware that my following question is typescript specific, but I'm banking on@phryneas's generosity here ^^ I have the following code: import{createSlice,PayloadAction}from"@reduxjs/toolkit";exporttypeErrorSlice=|{showErrorModal:false}|{showErrorModal:true;errorTitel:string;errorBody:string};constinitialState={showErrorModal:false,errorBody:null,errorTitel:null}asErrorSlice;const{reducer:uiErrorReducer,actions:uiErrorActions}=createSlice({name:"ui/error",initialState:initialState,reducers:{hideErrorModal:()=>initialState,showErrorModal:(state,action:PayloadAction<{errorTitel:string;errorBody:string}>)=>({showErrorModal:true, ...action.payload}),},});export{uiErrorActions,uiErrorReducer}; I am wondering why TS isn't throwing an error for declaring constinitialState={showErrorModal:false,errorBody:null,errorTitel:null}asErrorSlice; as the What am I missing here? 🤔 |
@polaroidkidd interfaces only describe a shape that should be matched, but don't restrict other properties from being present. If you want that, you have to exporttypeErrorSlice=|{showErrorModal:false;errorTitel:never;errorBody:never}|{showErrorModal:true;errorTitel:string;errorBody:string}; |
polaroidkidd commentedJan 7, 2021
oh wow! I wasn't aware of the |
danielo515 commentedMar 23, 2021
Doesn't have typescript a way to define "exact" objects? Flow has this notation |
danielo515 commentedMar 24, 2021
Thanks for the link. |
see#826 and#735 - copying from the latter why this is neccessary:
This is a TypeScript "feature" that is a bit unfortunate and that I've noticed in recent versions:
TS actually does some code flow analysis and sometimes "tightens" types that you yourself declared wider.
Doing
would work as well here, as it is an implicit cast.
We might need to check our docs for this in a few places.
Originally posted by@phryneas in#735 (comment)