-
Notifications
You must be signed in to change notification settings - Fork 28
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
basic typescript definitions #18
Open
bsouthga
wants to merge
3
commits into
stasm:master
Choose a base branch
from
bsouthga:typescript-definitions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* html string helper function, flattens array values | ||
*/ | ||
declare function html( | ||
components: TemplateStringsArray, | ||
...values: ( | ||
| boolean | ||
| void | ||
| null | ||
| string | ||
| number | ||
| (void | null | boolean | string | number)[])[] | ||
): string; | ||
|
||
/** | ||
* create application store from reducer | ||
*/ | ||
declare function createStore<State, Action>( | ||
reducer: (state: State, action: Action) => State | ||
): { | ||
/** | ||
* dispatch an app action | ||
*/ | ||
dispatch(action: Action, ...args: any[]): void; | ||
|
||
/** | ||
* mount a component on an element | ||
*/ | ||
attach<Component extends (...args: any[]) => string>( | ||
component: Component, | ||
root: Element | ||
): void; | ||
|
||
/** | ||
* attach the app state to a component | ||
*/ | ||
connect(component: (state: State) => string): () => string; | ||
connect<A>(component: (state: State, a: A) => string): (a: A) => string; | ||
connect<A, B>( | ||
component: (state: State, a: A, b: B) => string | ||
): (a: A, b: B) => string; | ||
connect<A, B, C>( | ||
component: (state: State, a: A, b: B, c: C) => string | ||
): (a: A, b: B, c: C) => string; | ||
connect(component: (state: State, ...args: any[]) => string): (...args: any[]) => string; | ||
}; | ||
|
||
export { createStore }; | ||
export default html; | ||
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. Nit: please add a newline at the end of the file here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
With this line here, are the other declarations using generics needed?
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.
This is sort of an unfortunate property of typescript lacking "variadic kinds", which is basically typed spread arguments.
If we only have the last signature, than any connected components will always have the function signature
(...args: any[]) => string
instead of specific argument types.For example, say we have a component like the following
and then connect 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.
the current annotation basically says... If the connected function has 3 or fewer arguments, create a new function with the same arguments (minus
state
), otherwise just type it as(...args: any[]) => string
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.
Thanks for the explanation. This makes sense to me now.