-
-
Notifications
You must be signed in to change notification settings - Fork 15.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
Incomplete comment for AnyAction in index.d.ts #3314
Comments
Yes, we need As for the comment in question, I'm not actually sure where that was going. @aikoven, any chance you remember? |
I think that it has to be something like
|
The actual PR that added |
You could easily add the same index signature to Action though. In other words, Action and AnyAction could easily just be one type. I don't really see the benefit of not doing this.
I actually don't really understand the reasoning behind defaulting Right now, the tests in
or
I think it would make more sense to default to the most common use case, I think the types & the tests for the types could be improved in other ways too, but that's probably better for a separate conversation. Back to the AnyAction comment:
Why do we want to do this? You should be able to to access the
Yes, you don't need an index signature to access that property, since you can just do |
You could also go further:
if you wanted to allow the user to better lock down the possible value types in the action. This seems like a better use case for defaulting a generic to |
This would kill type safety. Imagine you had an action like: interface MyAction extends Action {
payload: number;
} With index signature, TS won't catch errors like this: const action: MyAction = ...
action.payloa; // mistake, not caught by the compiler
Redux may be used in lots of different ways, and it's not true that you'd always want to narrow the type of
This way we'd make actions look like dictionaries, which they are generally not. |
Ah, sorry. You're right. I just tried this. I didn't realize TS would allow any extra misc properties here. I was thinking it was solely about index access for annotated properties & the types of the values assigned to them for some reason. I guess the index signature is more about using Records to dynamically perform lookups based on IDs, like string UUIDs.
I'm not sure I follow you here. Why would you ever not want the capability to narrow down action types in reducers (based on the type property)? In a statically typed environment, you want to know which action you're working with so that your update logic is safe. You may not always need to narrow down the action types, depending on your update logic (like if you want to do the same thing in response to a variety of different actions), but I'm having trouble thinking of how always having that capability would be a bad thing.
What do you mean? The value may be unknown (if dynamically generated), but you would, at least, know the type of that value. Side-note -- It is possible to dynamically generate unique types with some type system trickery to simulate nominal typing. See here if interested: https://gist.github.com/joshburgess/729a2aeb7a9d6c09e3ac5a95cd2071be
Why would
Or, if you wanted specific literals (more useful, because it allows you to narrow down the action types in reducers), something like:
These things could even be inferred via action creators generically if the action creator functions themselves used generics and passed them along to the return type. I think it would be extremely rare to want
However, you were right with the first point about this index signature allowing extra properties, and that definitely is a problem & a good reason not to include that index signature on the base Sorry if this is too off topic vs the original post. Maybe, I should make a PR to fix that |
Here is that PR for the |
Do you want to request a feature or report a bug?
Bug -- Sort of... it's an incomplete comment in index.d.ts
What is the current behavior?
The comment above
AnyAction
is incomplete. See here:This sentence never finishes, failing to explain the reasoning for the AnyAction type to exist.
This is not part of `Action` itself to prevent users who are extending `Action.
...???What is the expected behavior?
This comment should finish its sentence and be more clear.
I can submit a PR once told what this is supposed to say, but I'm not quite sure just by looking at it... It's obvious the rest of the sentence was not included, but not obvious what it was going to say.
However, honestly, I'm not quite sure having
AnyAction
makes much sense here. It appears to only be adding an index signature to the baseAction
type, but I think we actually want that index signature onAction
too, as we should be able to do things likesomeAction['type']
.... and, if that's the case, what doesAnyAction
even do for us here? TypeScript does not enforce exactly matching properties in object types. It only requires us to meet the requirements of the interface/type, ignoring any extra properties. So, we can still include extra properties with theAction
type.This leaves me wondering... do we even need this
AnyAction
type at all?The text was updated successfully, but these errors were encountered: