Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
phryneas merged 1 commit intoreduxjs:masterfromphryneas:docs-ts-initialState
Dec 23, 2020

Conversation

@phryneas
Copy link
Member

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:

functionidentity<T>(t:T){returnt}typeSliceState={state:"loading"}|{state:"finished";data:string};letinitialState:SliceState={state:"loading"};// is inferred as  { state: "loading"; }constderivedValue=identity(initialState)

TS actually does some code flow analysis and sometimes "tightens" types that you yourself declared wider.

Doing

constinitialState={state:"loading"}asSliceState;

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)

koojaa, thepouria, and mj2068 reacted with thumbs up emojisably and Folvin reacted with thumbs down emojiVoyager101pw, Ilyacnt, and Jomni-Mohamed-Amine reacted with heart emoji
@netlify
Copy link

netlifybot commentedNov 29, 2020

Deploy preview forredux-starter-kit-docs ready!

Built with commit2a22180

https://deploy-preview-827--redux-starter-kit-docs.netlify.app

@codesandbox-ci
Copy link

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:

SandboxSource
VanillaConfiguration
Vanilla TypescriptConfiguration
rsk-github-issues-exampleConfiguration

Copy link
Member

@msutkowskimsutkowski left a 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 👍

@phryneasphryneas merged commit8ba64bc intoreduxjs:masterDec 23, 2020
@phryneasphryneas deleted the docs-ts-initialState branchDecember 23, 2020 23:28
@phryneas
Copy link
MemberAuthor

completely forgot to merge this one 🤦

@polaroidkidd
Copy link

quick question: does this mean that if I declare my state as suggested I'll have to declare every possible state permutation?

@phryneas
Copy link
MemberAuthor

@polaroidkidd only if you want your state to accept only very specific shapes.
If you are content with{ state: string; data?: string };, just use that.
But if you wantdata to only be accessible whenstate is"finished", you'd need to go for a union type. But that's nothing redux-specific at all, that's just TypeScript.

polaroidkidd reacted with thumbs up emoji

@polaroidkidd
Copy link

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 theErrorSlice type does not declare a permutation that allows forshowErrorModal: false anderrorBody: null &errorTitle: null. I can even set the error title/Body to contain strings ("example"), but the initial state is accepted as is.

What am I missing here? 🤔

@phryneas
Copy link
MemberAuthor

@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, i9or, and DercilioFontes reacted with heart emoji

@polaroidkidd
Copy link

oh wow! I wasn't aware of thenever property. This will be super useful! Thank you very much!

@danielo515
Copy link

Doesn't have typescript a way to define "exact" objects? Flow has this notation{| prop: boolean |} which will not allow extra props, just that.

@phryneas
Copy link
MemberAuthor

@danielo515
Copy link

Thanks for the link.
What a shame

@phryneasphryneas restored the docs-ts-initialState branchMay 31, 2021 14:40
@reduxjsreduxjs deleted a comment fromsablyAug 24, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@msutkowskimsutkowskimsutkowski approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@phryneas@polaroidkidd@danielo515@msutkowski

[8]ページ先頭

©2009-2025 Movatter.jp