Repository: recharts/recharts
File: src/state/selectors/combiners/combineTooltipPayload.ts (TypeScript)

Code before:
import {
  TooltipEntrySettings,
  TooltipIndex,
  TooltipPayload,
  TooltipPayloadConfiguration,
  TooltipPayloadEntry,
  TooltipPayloadSearcher,
} from '../../tooltipSlice';
import { ChartData, ChartDataState } from '../../chartDataSlice';
import { DataKey, TooltipEventType } from '../../../util/types';
import { findEntryInArray } from '../../../util/DataUtils';
import { getTooltipEntry, getValueByDataKey } from '../../../util/ChartUtils';
import { getSliced } from '../../../util/getSliced';
import { ActiveLabel } from '../../../synchronisation/types';

type TooltipPayloadItemLike = {
  name: TooltipEntrySettings['name'];
  unit: TooltipEntrySettings['unit'];
  dataKey: DataKey<any> | undefined;
  payload: unknown;
  color: string | undefined;
  fill: string | undefined;
};

function isRecord(value: unknown): value is Record<string, unknown> {
  return value != null && typeof value === 'object';
}

function parseName(value: unknown): TooltipEntrySettings['name'] {
  if (typeof value === 'string' || typeof value === 'number') {
    return value;
  }
  return undefined;
}

function parseUnit(value: unknown): TooltipEntrySettings['unit'] {
  if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
    return value;
  }
  return undefined;
}

function parseDataKey(value: unknown): DataKey<any> | undefined {
  if (typeof value === 'string' || typeof value === 'number') {
    return value;
  }
  if (typeof value === 'function') {
    return obj => value(obj);
  }
  return undefined;

Review comment (suggestion):
And, you're not going to believe this, but the `is` keyword _also_ disables the type system completely, same as @ts-expect-error and `as`. At least the runtime check is right this time.

Code after:
import {
  TooltipEntrySettings,
  TooltipIndex,
  TooltipPayload,
  TooltipPayloadConfiguration,
  TooltipPayloadEntry,
  TooltipPayloadSearcher,
} from '../../tooltipSlice';
import { ChartData, ChartDataState } from '../../chartDataSlice';
import { DataKey, TooltipEventType } from '../../../util/types';
import { findEntryInArray } from '../../../util/DataUtils';
import { getTooltipEntry, getValueByDataKey } from '../../../util/ChartUtils';
import { getSliced } from '../../../util/getSliced';
import { ActiveLabel } from '../../../synchronisation/types';

type TooltipPayloadItemLike = {
  name: TooltipEntrySettings['name'];
  unit: TooltipEntrySettings['unit'];
  dataKey: DataKey<any> | undefined;
  payload: unknown;
  color: string | undefined;
  fill: string | undefined;
};

function parseName(value: unknown): TooltipEntrySettings['name'] {
  if (typeof value === 'string' || typeof value === 'number') {
    return value;
  }
  return undefined;
}

function parseUnit(value: unknown): TooltipEntrySettings['unit'] {
  if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
    return value;
  }
  return undefined;
}

function parseDataKey(value: unknown): DataKey<any> | undefined {
  if (typeof value === 'string' || typeof value === 'number') {
    return value;
  }
  if (typeof value === 'function') {
    return obj => value(obj);
  }
  return undefined;
}

function parseColor(value: unknown): string | undefined {
  if (typeof value === 'string') {