diff --git a/types/react-tracking/index.d.ts b/types/react-tracking/index.d.ts index 9b03b98275..7d59d650d7 100644 --- a/types/react-tracking/index.d.ts +++ b/types/react-tracking/index.d.ts @@ -1,13 +1,14 @@ -// Type definitions for react-tracking 6.0 +// Type definitions for react-tracking 7.0 // Project: https://github.com/NYTimes/react-tracking // Definitions by: Eloy Durán +// Christopher Pappas // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 import * as React from "react"; -export interface TrackingProp { - trackEvent({}): any; +export interface TrackingProp

{ + trackEvent(data: Partial

): any; /** * This method returns all of the contextual tracking data up until this point in the component hierarchy. @@ -55,15 +56,26 @@ export type TrackingInfo = T | ((props: P, state: S, args: any[any]) => // Duplicated from ES6 lib to remove the `void` typing, otherwise `track` can’t be used as a HOC function that passes // through a JSX component that be used without casting. type ClassDecorator = (target: TFunction) => TFunction; -type MethodDecorator = (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor; +type MethodDecorator = ( + target: object, + propertyKey: string | symbol, + descriptor: TypedPropertyDescriptor +) => TypedPropertyDescriptor; export type Decorator = ClassDecorator & MethodDecorator; +/** + * A React context used to support passing and dispatching tracking data throughout a tree of components. + */ export type TrackingContext = React.Context<{ - tracking: Options & { data?: {} } + tracking: Options & { data?: {} }; }>; - export const ReactTrackingContext: TrackingContext; +/** + * A React hook used to tap into the tracking context. + */ +export function useTracking

(): TrackingProp

; + /** * This is the type of the `track` function. It’s declared as an interface so that consumers can extend the typing and * specify defaults, such as a global analytics schema for the tracking-info. diff --git a/types/react-tracking/test/react-tracking-with-types-tests.tsx b/types/react-tracking/test/react-tracking-with-types-tests.tsx index 252c71e500..a2d6f2809c 100644 --- a/types/react-tracking/test/react-tracking-with-types-tests.tsx +++ b/types/react-tracking/test/react-tracking-with-types-tests.tsx @@ -1,5 +1,6 @@ import * as React from 'react'; -import { Track, track as _track, TrackingProp, Options, Decorator, TrackingContext, ReactTrackingContext } from 'react-tracking'; +import { Track, track as _track, TrackingProp, Options, Decorator, TrackingContext, ReactTrackingContext, useTracking } from 'react-tracking'; +import { string } from 'prop-types'; function customEventReporter(data: { page?: string }) {} @@ -85,3 +86,17 @@ const TestContext = () => { ); }; + +interface Trackables { + page: string; + app: string; +} + +const App = track()((props: { foo: string }) => { + const tracking = useTracking(); + return

{ + tracking.trackEvent({ + page: 'Home' + }); + }}/>; +}); diff --git a/types/react-tracking/test/react-tracking-without-types-tests.tsx b/types/react-tracking/test/react-tracking-without-types-tests.tsx index a3cf2b4330..2400641e83 100644 --- a/types/react-tracking/test/react-tracking-without-types-tests.tsx +++ b/types/react-tracking/test/react-tracking-without-types-tests.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { Track, track, TrackingProp } from 'react-tracking'; +import { Track, track, TrackingProp, useTracking } from 'react-tracking'; function customEventReporter(data: { page?: string }) {} @@ -55,3 +55,13 @@ class Test extends React.Component { ); } } + +const App = track()(({ foo }: { foo: string }) => { + const tracking = useTracking(); + return
{ + tracking.trackEvent({ + page: 'Home', + foo + }); + }}/>; +});