import { EventHandler, SyntheticEvent } from 'react' import { EventsHandlers, SvgAttributes } from './types.svg' export const tuple = (...args: Args) => args export const strTuple = (...args: Args) => args export const isUndefinedTuple = ( x: [any, any] ): x is [undefined, undefined] => { return x.length === 2 && x[0] === undefined && x[1] === undefined } export const isNotNil = (t: T | undefined | null): t is T => { return t !== null && t !== undefined } /** * Type used to prettify complex type */ type Id = { [K in keyof T]: T[K] } type Get = Key extends keyof From ? From[Key] : never /** * Picks undefined keys */ type UndefinedProperties = { [P in keyof T]-?: undefined extends T[P] ? P : never }[keyof T] /** * Turn undefined keys as optional */ type ToOptional = Id< Partial>> & Omit> > /** * Merge objects */ export type Merge = ToOptional< { [K in keyof A | keyof B]: Get | Get } > export type Component = (props: Props) => JSX.Element export type SvgAttributesGetters = { [K in keyof SvgAttributes]?: Getter> } type NativeEvent = T extends EventHandler ? V extends SyntheticEvent ? E : never : never export type NativeEventHandlers = { [Event in keyof EventsHandlers]?: ( event: NativeEvent[Event]>, datum: T ) => void } export type Iteratee = (t: T, i: number) => U export type DefaultedIteratee = U | Iteratee export function wrapIteratee( iteratee: Iteratee, map: Accessor ): Iteratee { return (d: A, i: number) => iteratee(map(d), i) } export interface CommonAnimationProps { delay: number duration: number easing: (time: number) => number } export interface AnimationIteratees { delay?: DefaultedIteratee duration?: DefaultedIteratee easing?: (time: number) => number } export interface AnimationProps extends AnimationIteratees { dataKey?: KeyAccessor enter?: SvgAttributes } export type Accessor = (t: Datum) => Value export type Getter = Value | Accessor export function isAccessor( getter: Getter ): getter is Accessor { return typeof getter === 'function' } export function toAccessor(getter: Getter): Accessor { return isAccessor(getter) ? getter : () => getter } export type DataValue = string | number | Date export type DataAccessor = Accessor export type DataGetter = Getter export type KeyAccessor = (t: T, index: number) => DataValue export type Filter = (value: T, index: number, array: T[]) => boolean export type ScaleContinuous = ((value: number | Date) => number) & { invert: (value: number) => number | Date nice(): ScaleContinuous copy(): ScaleContinuous ticks(count?: number): number[] | Date[] range(): number[] } export type ScaleCategorical = ((value: string) => number) & { copy(): ScaleCategorical bandwidth(): number domain(): string[] range(): number[] } export type ScaleOrdinal = (value: string) => string export const isScaleContinuous = (v: any): v is ScaleContinuous => { return ( typeof v === 'function' && typeof v.invert === 'function' && typeof v.nice === 'function' && typeof v.copy === 'function' ) } export const isScaleCategorical = (v: any): v is ScaleCategorical => { return ( typeof v === 'function' && typeof v.copy === 'function' && typeof v.bandwidth === 'function' ) } export type CartesianScale = ScaleContinuous | ScaleCategorical