-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat(vanilla): non-object state #1144
Conversation
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 bfde4c2:
|
Size Change: +430 B (+1%) Total Size: 29.4 kB
ℹ️ View Unchanged
|
@devanshj Can you help typing middlewares? This is not in hurry. |
I think things should not change much, this would mainly involve removing |
Yeah, I already tried and lost confident, especially around |
Ah okay then I'll look into it hopefully soon.
Yeah if you can tell me specific places where you got stuck then I can solve it and explain it to you and it will also make you understand the types better. |
A extends Action, | ||
Cms extends [StoreMutatorIdentifier, unknown][] = [] | ||
>( | ||
type Redux = <T, A, Cms extends [StoreMutatorIdentifier, unknown][] = []>( |
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.
@dai-shi Is removing the constraint in A
intentional? This would allow non-redux actions (and non-redux reducers), which might not work well with devtools
. I don't think it's a big deal to remove the constraint, but I'd prefer to keep it.
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.
Yes, this is intentional. (to remove Cast
, which is more important for me.)
For 99% use cases, there should be no issues. btw, we have a type guard and work around to force { type: unknown }
.
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.
to remove
Cast
, which is more important for me.
Instead of Cast<A, Action>
we can write A & Action
too1. We just have to satisfy the type-checker. Both Cast<A, Action>
and A & Action
are subtype of Action
(which is what the type-checker wants), and when A
is subtype of Action
(which it'll always be) both mathematically resolve to just A
. And we'll have to write A & Action
at only one place.
btw, we have a type guard and work around to force
{ type: unknown }
.
That makes it worse, right now we're sending isObjectWithTypeProperty(x) ? x : { type: x }
that means if someone has an action { kind: "INCREMENT", by: 10 }
(which is now allowed) what will get sent is { type: { kind: "INCREMENT", by: 10 } }
.
For 99% use cases, there should be no issues.
That's true. It's not a huge problem, it's just a matter of correctness. And again, I'm fine eitherway.
Footnotes
-
In case one is wondering why we used
Cast
and not&
in the first place...Cast
is prefered because even thoughCast<A, Action>
andA & Action
both mathematically resolve to justA
, theA & Action
will still show& Action
part in the quickinfo tooltip (which is useless becauseA
is already a subtype ofAction
) whereasCast<A, Action>
literally (not just mathematically) resolves toA
becauseA
is returned as it is. So the answer is "to make the quickinfo nicer". ↩
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.
if someone has an action
{ kind: "INCREMENT", by: 10 }
(which is now allowed)
I implicitly and intentionally make it loose. Redux Devtools allows { type: { kind: "INCREMENT", by: 10 } }
, right?
(So, I really wanted to make State=unknown
and Action=unknown
.)
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.
Redux Devtools allows
{ type: { kind: "INCREMENT", by: 10 } }
, right?
Yes but the action is { kind: "INCREMENT", by: 10 }
and not { type: { kind: "INCREMENT", by: 10 } }
. So when the user jumps or dispatches an action from the redux devtools, what we'll receive from the extenstion is { type: { kind: "INCREMENT", by: 10 } }
which we'll dispatch and forward to the reducer but the reducer will simply won't recognise it.
The point I'm trying to make is all tooling, libraries, code around redux is written keeping in mind that the action will have a type
property, so we can't allow violating the redux spec.
I personally see no point in violating the spec just for removing & Action
from our code. But I'm fine if you're making that choice consciously. In that case maybe let's send typeof x === "object" ? x : { type: x }
instead.
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.
which we'll dispatch and forward to the reducer but the reducer will simply won't recognise it.
Ah, I overlooked that.
Hm, I thought "our" redux middleware doesn't need to follow the type
property spec, but this your comment makes me change the idea. Let me see how & Action
works.
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.
Let's also change the /**
- * @deprecated Use `object` instead of `State`
+ * @deprecated Use `unknown` instead of `State`
*/
- export type State = object
+ export type State = unknown This should be probably be non-breaking as we have changed |
* fix typo in jsdoc * change State type #1144 (comment)
* fix typo in jsdoc * change State type pmndrs/zustand#1144 (comment)
* fix typo in jsdoc * change State type pmndrs/zustand#1144 (comment)
and simplify types (eliminating
Cast
).There might not be many use cases, but maybe we can support non-object state.
Counter example: https://codesandbox.io/s/react-typescript-forked-uvyys3?file=/src/App.tsx
TODOs