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

Give elements their own interface definition #8290

Merged
merged 1 commit into from
Jan 30, 2021

Conversation

joshkel
Copy link
Contributor

@joshkel joshkel commented Jan 8, 2021

From what I understand, if we want to allow registering additional element options (see here for an example), then the element options need to be a top-level interface so that they can be used with TypeScript's declaration merging.

From what I understand, if we want to allow registering additional element options (see [here][1] for an example), then the element options need to be a top-level interface so that they can be used with TypeScript's [declaration merging][2].

[1]: chartjs/chartjs-plugin-annotation#275 (comment)
[2]: https://www.typescriptlang.org/docs/handbook/declaration-merging.html
@@ -1834,14 +1834,16 @@ export const BarElement: ChartComponent & {
new (cfg: any): BarElement;
};

export interface ElementOptions {
Copy link
Member

Choose a reason for hiding this comment

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

When I read ElementOptions, I'm thinking of options for an element, and I would expect to be able to extend it for each element type (i.e. interface ArcOptions extends ElementOptions). Shouldn't this be named differently then, maybe ElementsOptions or ElementOptionsMap or something else that makes it clear it's a record of element type -> element options (ElementOptionsByType)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback.

I see your point. For what it's worth, the name ElementOptions would match the current PluginOptions (which seems to serve a similar purpose). Otherwise, I might pick ElementOptionsMap (seems clearest to my personal taste) or ElementsOptions (to be a bit more consistent with pre-existing type definitions' names).

I don't have a strong opinion, so I'll defer to others.

After taking another look at this, I realized that I could probably set up a ...Registry interface; this should serve the same purpose and would be similar to Chart.js's ScaleTypeRegistry and ChartTypeRegistry.

export interface ElementTypeRegistry {
        arc: ArcOptions & ArcHoverOptions;
        bar: BarOptions & BarHoverOptions;
        line: LineOptions & LineHoverOptions;
        point: PointOptions & PointHoverOptions;
 }
 export interface ElementChartOptions {
       elements: { [k in keyof ElementTypeRegistry]: ElementTypeRegistry[k]; };
 }

Or, using an options sub-object to match the other ...TypeRegistries: (This doesn't seem to be necessary for elements, but it's consistent and allows room for future growth.)

export interface ElementTypeRegistry {
	arc: {
		options: ArcOptions & ArcHoverOptions;
	}
	bar: {
		options: BarOptions & BarHoverOptions;
	}
	line: {
		options: LineOptions & LineHoverOptions;
	}
	point: {
		options: PointOptions & PointHoverOptions;
	}
}
export interface ElementChartOptions {
	elements: { [k in keyof ElementTypeRegistry]: ElementTypeRegistry[k]['options']; };
}

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, same problem with PluginOptions, ScaleOptions (and maybe some others). For this last, it's even more confusing because there is CoreScaleOptions. There is also CommonOptions which is too generic ("common" for what?) and should be renamed since it's not an interface that's supposed to be extend by all options set.

I would reserve these {Type}Options (i.e. PluginOptions, ScaleOptions, ElementOptions, etc.) for base interfaces, containing common properties for each type (if needed). We could prefix them to make explicit that these are "base" interfaces but I'm not sure I would pick Core (maybe Base instead).

I'm not convinced by ElementOptionsMap finally because it's not a JavaScript Map, so could be confusing. I'm not familiar with these *Registry types, but if it's a common pattern in our TD, then I guess it's a good one. Though, it would need to contain Options in the name (maybe ElementOptionsRegistry) because we can also have a registry for element (not element options). Then we should also rename PluginOptionsRegistry, ScaleOptionsRegistry, etc.

I kind like/prefer my last suggestion: ElementOptionsByType (PluginOptionsByType, ScaleOptionsByType) because it's clear that's a plain object containing a mapping between the type name and options. With Registry, it's a bit confusing because I understand "registry" as an object containing some logic / extra methods (i.e. ScaleOptionsRegistry.register()).

Finally, I would rename ElementChartOptions as well (or even better, remove it since I find it useless and making hard to understand the type hierarchy.

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer ElementTypeOptions over ElementOptionsByType. Not a strong opinion, just personal preference on naming conventions.

@kurkle
Copy link
Member

kurkle commented Jan 20, 2021

Should the naming changes be done in a separate commit? This is getting stale.

@etimberg
Copy link
Member

I can followup with the naming changes if we want to merge this @kurkle.

@kurkle
Copy link
Member

kurkle commented Jan 30, 2021

I can followup with the naming changes if we want to merge this @kurkle.

Works for me

@kurkle kurkle added the type: types Typescript type changes label Jan 30, 2021
@etimberg etimberg added this to the Version 3.0 milestone Jan 30, 2021
@etimberg etimberg merged commit 5eb3682 into chartjs:master Jan 30, 2021
@etimberg etimberg mentioned this pull request Jan 30, 2021
@joshkel joshkel deleted the element-augmentation branch March 24, 2021 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: types Typescript type changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants