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

Conversation

bsouthga
Copy link
Contributor

Not sure if this is something you want in this lib @stasm, but these are the type definitions I used for innerself-hn and thought others might find them useful.

Alternatively we could publish them on DefinitivelyTyped, but its always nice to have them in the repo.

Copy link
Owner

@stasm stasm left a comment

Choose a reason for hiding this comment

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

Cool, I'm all for adding this to the repo, thanks! I don't have much experience with TypeScript, so excuse me if my question below is silly ^^.

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.

@bsouthga
Copy link
Contributor Author

one other thought on this, might want to wait until we figure out if connect might change after discussion in #19

@haoxins
Copy link

haoxins commented Jul 18, 2018

@stasm can we merge this PR now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants