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

🔨 (grapher) replace some enum types / TAS-709 #4170

Merged
merged 2 commits into from
Nov 26, 2024

Conversation

sophiamersmann
Copy link
Member

@sophiamersmann sophiamersmann commented Nov 14, 2024

Refactors Grapher types by replacing enums with compile-time-only types.

I introduced GrapherTabName in the previous PR, which is either a ChartTypeName or "WorldMap" or "Table". But enums currently can't be extended or merged, which means that I had to copy-paste all chart type names into GrapherTabName. As a result, TypeScript doesn't do a good job of type narrowing, forcing me to write (unnecessary) type assertions in many places.

This PR refactors ChartTypeName and other tab-related types so they don't use enums. However, I still wanted the convenience of a single object to access chart types, for example. To get that, the types are inferred from const objects:

const GRAPHER_CHART_TYPES = {
    LineChart: "LineChart",
    ScatterPlot: "ScatterPlot"
} as const

type GrapherChartType = keyof typeof GRAPHER_CHART_TYPES

I first did it the other way around (first defining the types, then using them for the objects), but it turns out the way it's done now leads to better type narrowing.

Newly introduced types:

  • GrapherMapType: only "WorldMap"
  • GrapherChartType: "LineChart", "ScatterPlot", etc. (without "WorldMap")
  • GrapherChartOrMapType: GrapherChartType or GrapherMapType
  • GrapherTabName: Internal tab names used in Grapher (a chart type name or "WorldMap" or "Table")
  • GrapherTabOption: Grapher tab specified in the config that determines the default tab to show ("chart", "map" or "tab")
  • GrapherTabQueryParam: Valid values for the tab query parameter in Grapher ("chart", "map", "line", "slope", etc.)

@sophiamersmann sophiamersmann changed the title refactor types 🔨 (grapher) refactor ChartTypeName and GrapherTabOption / TAS-709 Nov 14, 2024
Copy link

@sophiamersmann sophiamersmann changed the title 🔨 (grapher) refactor ChartTypeName and GrapherTabOption / TAS-709 🔨 (grapher) refactor types around chart&map types and tab options / TAS-709 Nov 14, 2024
@owidbot
Copy link
Contributor

owidbot commented Nov 14, 2024

Quick links (staging server):

Site Admin Wizard Docs

Login: ssh owid@staging-site-grapher-refactor-types

SVG tester:

Number of differences (default views): 1366 (4a0376) ❌
Number of differences (all views): 0 ✅

Edited: 2024-11-26 09:42:26 UTC
Execution time: 1.26 seconds

@sophiamersmann sophiamersmann changed the title 🔨 (grapher) refactor types around chart&map types and tab options / TAS-709 🔨 (grapher) replace some enum types / TAS-709 Nov 18, 2024
@sophiamersmann sophiamersmann force-pushed the grapher-refactor-types branch 2 times, most recently from cdc7ecf to fa5b967 Compare November 26, 2024 09:21
Copy link
Member Author

sophiamersmann commented Nov 26, 2024

Merge activity

  • Nov 26, 4:52 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Nov 26, 4:55 AM EST: Graphite rebased this pull request as part of a merge.
  • Nov 26, 4:56 AM EST: A user merged this pull request with Graphite.

@sophiamersmann sophiamersmann changed the base branch from grapher-types-viz to graphite-base/4170 November 26, 2024 09:52
@sophiamersmann sophiamersmann changed the base branch from graphite-base/4170 to master November 26, 2024 09:52
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