-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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 typings #7655
Comments
Thanks for leading the charge on this!
I personally don't have a strong opinion on this except to say that I'd like it if we could also add tests for the typings and consider the directory structure of where the tests will go
The code might be the best source for this. The only other thing we have is https://www.chartjs.org/docs/master and https://www.chartjs.org/docs/master/typedoc. I believe @stockiNail has been going through and trying to identify places where the docs do not match the code |
honestly, now sure how to test the typings. There is
However, I don't know how to test whether JavaScript matches the TypeScript typings |
I'm not an expert in this area, but here's what I've seen done in one of the plugins: https://github.com/chartjs/chartjs-plugin-datalabels/tree/master/types/test |
Awesome that this is happening! This is my sketch of how the types could potentially be structured: https://github.com/jonrimmer/Chart.js/tree/types-experiment
For this I more or less copied-and-pasted and the types from |
One thing that would be nice in the typings would be to allow templating the type DataPoint = {
x: number;
y: number;
};
type MyDataset = {
label: string;
data: DataPoint[];
};
type MyChartData = {
datasets: MyDataset[];
};
...
const chart = new Chart<MyChartData>(ctx, {
data,
});
// Now chart.data is of type MyChartData Another problem I ran into with the existing types is that a lot of things are marked as potentially undefined (e.g. |
In terms of getting types into master, rollup-plugin-dts looks pretty good. My only concern would be with how the |
That's a good point. I see three options:
// core/index.d.ts
interface ChartOptions {
responsive?: boolean;
// ...
} // plugins/index.d.ts
interface ChartOptions {
legend?: ChartLegendOptions;
} And since these are both bundled into the same file, declaration merging would create a unified interface. Unfortunately, rollup detects the duplicate names and mangles one of them, because it's really intended for bundling JS, where this kind of a collision is a problem. A workaround is to use module augmentation: // plugins/index.d.ts
declare module "./chart.js" {
interface ChartOptions {
legend?: ChartLegendOptions;
}
} The augmentation is emitted as-is into the defs bundle. It's a bit weird for a module to augment itself like this, but it does seem to work. Another possibility would be to use a non-rollup based tool such as dts-bundle-generator, as I believe it leaves duplicates unchanged. |
my current state: https://github.com/sgratzl/Chart.js/tree/sgratzl/types/types went through the code and now looking at the documentation for different options |
@sgratzl I'm working on animation options for another project. If I may provide a first feedack, having a look to the https://github.com/sgratzl/Chart.js/blob/sgratzl/types/types/core/index.d.ts, I see that the |
@sgratzl another feedback, always about animation.
|
thx, it is work in progress and I appreciate any hints |
Noticed a typo there: (data vs date) export const _adapters: {
_data: DateAdapter;
}; |
@sgratzl I like the type structure. What do you think the best way to collaborate on the types? We could split the initial types into separate modules + add the build steps then PR that followed by PRs for improvements. Or we could work and get the types all ready in one branch and merge at the end |
I should be able to finish going through the documentation by the end of today. Unfortunately, I don't get supported financially by anyone for doing things like that. Then I should have a list of all the properties. However, there are still open questions regarding how to mix the options and so on together. Like in the best case, as soon as I say it is a 'bar' controller, it should just suggest me the options that are valid. Another big questions is how to handle new plugins such that they can contribute to the configuration / options, ... |
Sounds good; I do this in my free time as well, so no rush 😄 Agree that once we say it's a bar controller, we would show the correct options. In my experience with typescript, that means we're going to need a bunch of derived types (not sure how we flow that down to the dataset options either). That's a great point on plugins. In theory, plugin options could be anywhere which really complicates the types |
so https://github.com/sgratzl/Chart.js/blob/sgratzl/types/types now contains all possible options I could find. However, what is missing is how to merge them to create one configuration |
https://github.com/chartjs/Chart.js/pull/7668/files#diff-7a581ab159bd53d91fc2a62a45759557 is a working version how the typing could work |
Isn't Chart.js v3 written in TypeScript? Is there any way to currently get typings for v3? |
No, it's not written in TypeScript, but some types were recently added: #7668 |
Oh, I'm a bit disappointed to hear it was not written in TS, I thought that was a big advantage of the rewrite. Can I use those typings through npm or do I have to manually copy the d.ts files? |
when you install the upcoming version of chart.js via npm, the typings should just work. |
You are referring to the v3-alpha-2? Because that doesn't have typings. Or do I have to wait for the next alpha release? |
No, the typings were merged after v3.0.0-alpha.2 released. I don't have a specific date targeted for the next release, but my gut says early September for a beta.1. To clarify as well, v2 -> v3 is not really a rewrite. While bits and pieces were rewritten (e.g. animations), by and large the v3 code is the v2 code. The major version bump comes from making a number of backwards incompatible changes to the config options. |
do overcome some issue in the current alpha version I created https://github.com/sgratzl/chartjs-esm-facade which also has the same Typescript typings |
I tried again upgrading now that 3.0.0-beta was released and typings are included, but after spending 1 hour trying to convert a single time-series chart I gave up. It keeps giving strange TypeScript errors that are hard to understand, and even while looking at the migration guide it is so hard to understand the correct format of the new chart options. One example that makes migration really hard is for example that Anyway, I do not recommend trying to update to v3.0-beta as the migration is really painful (I reverted my changes, couldn't make it work in 1 hour). |
Chart.js is made to work with plugins. I'm not sure we could define it as a union of literals because then you probably couldn't use a plugin chart like https://github.com/chartjs/chartjs-chart-financial |
Cool! I'll be excited to try these out! We should probably add some tests for these at some point as well |
Can someone give me some pointers on what is the correct typing to use for Chart.js v3 configuration options and data? The flurry of generics is really confusing me... |
With the latest beta release (v3.0.0-beta.3) this is the typing I am using in my typescript projects with regards to datasets. import { IChartDataset } from 'chart.js';
const dataset: IChartDataset<"line"> = {
// Validates that properties here match those expected for a line dataset.
}; |
Our application has been using Chart.js 3.0.0 alpha 1 with some hacked-together type definitions based on @types/chart.js, so I was excited to see that TypeScript type definitions will be built in. I've started looking at upgrading to the latest beta and switching to the build-in definitions. I'm really impressed with how thorough the definitions are and how well they support the various plot types and support extensibility. I have some questions and feedback. .d.ts organization: I'm in the habit of using WebStorm's "Go To Declaration or Usages" hotkey with third-party TypeScript definitions as a quick way of getting more information about an API. With the beta's definitions, that works very poorly; WebStorm goes to the imported-and-re-exported symbol within the barely-readable machine-generated Does Chart.js need to bundle its .d.ts files? In other words, instead of rolling up the Type names: I noticed that some of the type names are different than the third-party @types/chart.js. This seems fine to me - if the project is going to break any compatibility with type names, now seems like the time to do it, but I wanted to double-check that everyone's okay with this. (As long as I'm nitpicking, there is one place where I prefer @types/chart.js's names - it uses Type naming conventions: The beta's type definitions use the convention of And, now that I've stated my (strong) preference and reasoning, I'll defer to whatever you decide, since it's a subjective topic. 🙂 Specific questions: As part of updating my project, I've run into several problems or questions with the new type definitions. What's the best way to handle these (post a comment here, open a new issue listing all of them, open a separate issue for each, open a PR with proposed fixes for discussion, ...)?
I'm happy to open a PR for any of the above if it would help. Thanks for your work on this. It's great stuff. |
@joshkel thanks for the feedback! BundlesI have no strong preference on this. If we can avoid a build step, that's always nice Type Names / ConventionsFor the points, I believe the idea was that different chart types hence why they have different names. Specific Questions
|
I'd prefer to drop the |
I see there was some discussion around plugin options and how to leave the configuration types extensible, but couldn't understand what was the final decision there. At the moment, with the current types, the compiler would throw an error when trying to pass plugin options. |
@etimberg I'm looking at the PR submitted to port the datalabels pulgin to Chart.js v3 and I strongly agree that interfaces should not be prefixed with |
Good feedback @simonbrunel. Let's fix this before v3 releases |
About generic names (seen here): I'm not sure that's a common practice to write generic parameter full uppercase (whatever language actually, e.g. template in C++). I think uppercase are mostly understood as constants or defines. I like the explicit names, but I think |
Going to close this and use smaller tickets to address future issues. The types in v3 now are a big improvement over the v2 types. |
Feature Proposal
follow up from #6598 (comment)_
Feature Use Case
provide typescript typings directly as part of chart.js (3)
Possible Implementation
starting points are:
open questions:
The text was updated successfully, but these errors were encountered: