Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions index.d.ts
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;
Copy link
Owner

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?

Copy link
Contributor Author

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

const SayHi = (state: State, name: string) => html`
  <div>
    hey ${name}, check out this state prop ${ state.prop }
  </div>
`;

and then connect it

const ConnectedSayHi = connect(SayHi);

// if we only have the last signature, this will not error,
// as the signature is (...args: any[]) => string
ConnectedSayHi(10);

// however, if we have the over loaded signature, 
// then connected components with 3 or fewer arguments 
// will have their arguments typechecked...
ConnectedSayHi(10) // type error, as the signature is (a: string) => string;

Copy link
Contributor Author

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

Copy link
Owner

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.

};

export { createStore };
export default html;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please add a newline at the end of the file here.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.1",
"description": "A tiny view + state management solution using innerHTML",
"main": "index.js",
"typings": "index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/stasm/innerself.git"
Expand Down