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

Add typescript definitions #303

Merged
merged 11 commits into from
Sep 6, 2020
Merged

Add typescript definitions #303

merged 11 commits into from
Sep 6, 2020

Conversation

anajavi
Copy link
Collaborator

@anajavi anajavi commented Aug 19, 2020

what?

This adds typescript definition for the library. Not complete, but all series types should have types.

References highcharts types.

Somewhat related to discussion in #113 and in #267 (comment)

why?

  • Better IDE experience even when writing plain js.
  • Type checking for those who use typescript

caveats

  • Event handlers are not typed, instead the evented components accept any props in addition to highcharts options

I am no way expert in writing definition files. I suggest that we release this and see what happens? It's easy to remove if it causes too much a headache. Maybe adding something in the release notes about "beta types".

@anajavi anajavi requested a review from whawker August 19, 2020 09:59
@codecov-commenter
Copy link

codecov-commenter commented Aug 19, 2020

Codecov Report

Merging #303 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #303   +/-   ##
=======================================
  Coverage   88.27%   88.27%           
=======================================
  Files          82       82           
  Lines        1049     1049           
  Branches      206      206           
=======================================
  Hits          926      926           
  Misses        107      107           
  Partials       16       16           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 357cbee...7bf514b. Read the comment docs.

@veloek
Copy link
Contributor

veloek commented Aug 20, 2020

We've been maintaining our own typings for (a subset of) RJH, so this is really great!

You haven't considered moving the codebase towards typescript? I realise it's quite some work to get there, but I think it would be worth while. If not, please make sure to keep the typings up-to-date. Misleading types are worse than no types.

@anajavi
Copy link
Collaborator Author

anajavi commented Aug 20, 2020

We've been maintaining our own typings for (a subset of) RJH, so this is really great!

Can you please paste this definition to your module declaration and report if it works for you?

You haven't considered moving the codebase towards typescript? I realise it's quite some work to get there, but I think it would be worth while.

That's up to whawker to decide. Previously typescript was quite cumbersome to use with react, but latest releases have been better. It's however quite an effort and adds complexity.

@veloek
Copy link
Contributor

veloek commented Aug 20, 2020

Can you please paste this definition to your module declaration and report if it works for you?

We're using the react-jsx-highstock package and the highstock specific components are missing, but other than that it seems to work. There are a few any types though, which could be improved on further.

@anajavi
Copy link
Collaborator Author

anajavi commented Aug 20, 2020

There are a few any types though, which could be improved on further.

Do you happen to have definitions for those?

@veloek
Copy link
Contributor

veloek commented Aug 20, 2020

Well, I would have to go through the components and look at the code. One simple example is Credits where the props type should be Highcharts.CreditsOptions & { enabled?: boolean }.

@anajavi
Copy link
Collaborator Author

anajavi commented Aug 20, 2020

Well, I would have to go through the components and look at the code. One simple example is Credits where the props type should be Highcharts.CreditsOptions & { enabled?: boolean }.

I added type for <Credits /> in 19a4c68

@veloek
Copy link
Contributor

veloek commented Aug 21, 2020

Nice @anajavi, this is looking good! I would like to have the event handlers listed out though, instead of [x: string]: any, so they also get their appropriate types.

@anajavi
Copy link
Collaborator Author

anajavi commented Aug 21, 2020

Nice @anajavi, this is looking good! I would like to have the event handlers listed out though, instead of [x: string]: any, so they also get their appropriate types.

Something like this should probably work:

  type ChartProps = {
    onSelection?: Highcharts.ChartSelectionCallbackFunction;
    [x: string]: any; // TODO: this is here to allow eventhandlers like onAfterAddSeries
  } & Partial<Highcharts.ChartOptions>;

  export function Chart(props: ChartProps): ReactElement;

There are however more events in highcharts than there are in their typings. One example is afterAddSeries.

@anajavi
Copy link
Collaborator Author

anajavi commented Aug 21, 2020

Nice @anajavi, this is looking good! I would like to have the event handlers listed out though, instead of [x: string]: any, so they also get their appropriate types.

@veloek I added the event handler typings if you like to try.

@veloek
Copy link
Contributor

veloek commented Aug 21, 2020

@anajavi Great! Seems to work just fine :-)

Just wondering about the highstock package. Will that one get these typings? It will need some of it's own as well, but for typescript to figure out where to look based on the import, all types must be present in the highstock package as well.

@anajavi
Copy link
Collaborator Author

anajavi commented Aug 21, 2020

@anajavi Great! Seems to work just fine :-)

Just wondering about the highstock package. Will that one get these typings? It will need some of it's own as well, but for typescript to figure out where to look based on the import, all types must be present in the highstock package as well.

I think we should land react-jsx-highcharts types first. When the types have been released it is easier for somebody(you?) to make a PR for highstock.

@whawker
Copy link
Owner

whawker commented Aug 23, 2020

You haven't considered moving the codebase towards typescript?

To be honest I don't know Typescript, so I would struggle to maintain the project. It is something I intend to learn but I don't use it day-to-day currently, like I do with JS

I suggest that we release this and see what happens?

Worth releasing as a beta first?

@anajavi
Copy link
Collaborator Author

anajavi commented Aug 23, 2020

Worth releasing as a beta first?

This certainly won't break anybodys application, but might confuse a bit. Maybe a beta release and mention in the #259 might get some feedback?

I intend to add some kind of test or eslint configuration for this later so we see if new highcharts release breaks the typings(most likely the eventhandlers).

@anajavi anajavi merged commit 77d09c5 into master Sep 6, 2020
@anajavi anajavi deleted the feat/typescript branch September 6, 2020 16:02
@whawker
Copy link
Owner

whawker commented Oct 15, 2020

Apologies for the insane delay on this, life during Covid just ends up trying to keep way too many balls in the air at once.

I've published a beta react-jsx-highcharts@4.2.0-beta.1 containing the types added here.

Would those of you interested (@anajavi, @veloek) mind giving them a whirl?

@anajavi
Copy link
Collaborator Author

anajavi commented Oct 15, 2020

Works nicely at my end.

veloek added a commit to veloek/react-jsx-highcharts that referenced this pull request Jun 16, 2021
This builds on whawker#303 and extends types to also cover the
react-jsx-highstock package.

Please verify that the type definitions makes sense, @anajavi and and
@whawker. I used the types you defined, @anajavi, as reference and tried
to compare props of RJH components with Highcharts config types.

It works for our usage of RJH, but not all components are tested.
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.

4 participants