Repository: apache/superset
File: superset-frontend/src/explore/actions/hydrateExplore.ts (TypeScript)

Code before:
      initialFormData.include_time &&
      initialFormData.granularity_sqla &&
      !initialFormData.groupby?.some(
        (col: QueryFormColumn) =>
          getColumnLabel(col) ===
          getColumnLabel(initialFormData.granularity_sqla!),
      )
    ) {
      initialFormData.groupby = [
        initialFormData.granularity_sqla,
        ...ensureIsArray(initialFormData.groupby),
      ];
      initialFormData.granularity_sqla = undefined;
    }

    if (dashboardId) {
      initialFormData.dashboardId = dashboardId;
    }

    const initialDatasource = dataset;
    initialDatasource.currency_formats = Object.fromEntries(
      (initialDatasource.metrics ?? [])
        .filter(metric => metric.currency !== undefined)
        .map((metric): [string, Currency] => [
          metric.metric_name,
          metric.currency!,
        ]),
    );

    const initialExploreState = {
      form_data: initialFormData,
      slice: initialSlice,
      datasource: initialDatasource,
    };
    const initialControls = getControlsState(
      initialExploreState,
      initialFormData,
    ) as ControlStateMapping;
    const colorSchemeKey = initialControls.color_scheme && 'color_scheme';
    const linearColorSchemeKey =
      initialControls.linear_color_scheme && 'linear_color_scheme';
    // if the selected color scheme does not exist anymore
    // fallbacks and selects the available default one
    const verifyColorScheme = (type: ColorSchemeType) => {
      const schemes =
        type === 'CATEGORICAL'
          ? getCategoricalSchemeRegistry()
          : getSequentialSchemeRegistry();
      const key =
        type === 'CATEGORICAL' ? colorSchemeKey : linearColorSchemeKey;
      const registryDefaultScheme = schemes.defaultKey;

Review comment (suggestion):
I think the reason why typescript may have been complaining is because the value could still be null.

Code after:
      initialFormData.include_time &&
      initialFormData.granularity_sqla &&
      !initialFormData.groupby?.some(
        (col: QueryFormColumn) =>
          getColumnLabel(col) ===
          getColumnLabel(initialFormData.granularity_sqla!),
      )
    ) {
      initialFormData.groupby = [
        initialFormData.granularity_sqla,
        ...ensureIsArray(initialFormData.groupby),
      ];
      initialFormData.granularity_sqla = undefined;
    }

    if (dashboardId) {
      initialFormData.dashboardId = dashboardId;
    }

    const initialDatasource = dataset;
    initialDatasource.currency_formats = Object.fromEntries(
      (initialDatasource.metrics ?? [])
        .filter(metric => !!metric.currency)
        .map((metric): [string, Currency] => [
          metric.metric_name,
          metric.currency!,
        ]),
    );

    const initialExploreState = {
      form_data: initialFormData,
      slice: initialSlice,
      datasource: initialDatasource,
    };
    const initialControls = getControlsState(
      initialExploreState,
      initialFormData,
    ) as ControlStateMapping;
    const colorSchemeKey = initialControls.color_scheme && 'color_scheme';
    const linearColorSchemeKey =
      initialControls.linear_color_scheme && 'linear_color_scheme';
    // if the selected color scheme does not exist anymore
    // fallbacks and selects the available default one
    const verifyColorScheme = (type: ColorSchemeType) => {
      const schemes =
        type === 'CATEGORICAL'
          ? getCategoricalSchemeRegistry()
          : getSequentialSchemeRegistry();
      const key =
        type === 'CATEGORICAL' ? colorSchemeKey : linearColorSchemeKey;
      const registryDefaultScheme = schemes.defaultKey;