-
-
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
TypeScript definitions improvements #1526
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ const dispatchResult: Action = dispatch({type: 'TYPE'}); | |
|
||
type Thunk<O> = () => O; | ||
|
||
const dispatchThunkResult: number = dispatch(() => 42); | ||
const dispatchThunkResult: number = dispatch<Thunk<number>, number>(() => 42); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's what I'm talking about. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be written const dispatchThunkResult: number = dispatch((() => 42) as Thunk<number>); which is shorter, more explicit and clear of what it does, provides exactly the same type safety and doesn't require people to put type casts all over their code if they doesn't want to. Also, as you know what middlewares you have configured, for extra type safety you can cast the dispatch function instead (safer because you would only need to do this once so you drastically reduce the risk of miss typing). declare const dispatch: <T>(thunk: Thunk<T>) => T; As long as their isn't a way to make the types flow through all middlewares, that is the safest way. Adding type parameters which needs to be specified does not increase the safety at all as there is nothing that guarantees them to be correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Pajn , the point of generics is to provide typings over something that is user-defined, in this case the action and the result. While being able to infer them would be ideal, it is still just a convenience.
The argument is purely cosmetic and the increase of readability is arguable. It may sound a bit harsh, but which example being shorter is irrelevant, because that's not our goal. Otherwise we would be writing pure JavaScript instead. The main point here is that we end up with something sane, instead of an implicit getSomething<T>(): T; than getSomething(): any; While neither guarantees that you actually cast to the correct type, the first one at least ensures that you made a conscious decision, instead of accepting |
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.
it will not infer types automatically. can you define the function that will not require providing types explicitly?
If I call it without types, TS will infer
{}
as aTMiddlewareAction
.