You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c77e1cf
Browse filesBrowse the repository at this point in the historyBrowse files
🔨 (grapher) replace some enum types / TAS-709 (#4170)
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](microsoft/TypeScript#17592), 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:
```ts
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.)
0 commit comments