Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions types/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// Jessica Franco <https://github.com/Jessidhia>
// Paul Sherman <https://github.com/pshrmn>
// Saransh Kataria <https://github.com/saranshkataria>
// Sebastian Silbermann <https://github.com/eps1lon>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8

Expand All @@ -42,6 +43,15 @@ type NativeTransitionEvent = TransitionEvent;
type NativeUIEvent = UIEvent;
type NativeWheelEvent = WheelEvent;

/**
* defined in scheduler/tracing
*/
interface SchedulerInteraction {
id: number;
name: string;
timestamp: number;
}

// tslint:disable-next-line:export-just-namespace
export = React;
export as namespace React;
Expand Down Expand Up @@ -355,6 +365,26 @@ declare namespace React {
const Suspense: ExoticComponent<SuspenseProps>;
const version: string;

/**
* {@link https://github.com/bvaughn/rfcs/blob/profiler/text/0000-profiler.md#detailed-design | API}
*/
type ProfilerOnRenderCallback = (
id: string,
phase: "mount" | "update",
actualDuration: number,
baseDuration: number,
startTime: number,
commitTime: number,
interactions: Set<SchedulerInteraction>,
) => void;
interface ProfilerProps {
children?: ReactNode;
id: string;
onRender: ProfilerOnRenderCallback;
}

const unstable_Profiler: ExoticComponent<ProfilerProps>;

//
// Component API
// ----------------------------------------------------------------------
Expand Down
35 changes: 35 additions & 0 deletions types/react/test/tsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,41 @@ const ForwardRef3 = React.forwardRef(
<ForwardRef3 ref={divFnRef}/>;
<ForwardRef3 ref={divRef}/>;

const Profiler = React.unstable_Profiler;

// 'id' is missing
<Profiler />; // $ExpectError
// 'onRender' is missing
<Profiler id="test" />; // $ExpectError
// 'number' is not assignable to 'string'
<Profiler id={2} />; // $ExpectError

<Profiler
id="test"
onRender={(
id,
phase,
actualDuration,
baseDuration,
startTime,
commitTime,
interactions
) => {
const message = `${id} ${phase} took ${actualDuration.toFixed(2)}s actual, ${baseDuration.toFixed(2)}s base`;

const commitMessage = `commit started ${startTime.toFixed(2)} within ${commitTime}`;

const interactionsSummary = Array.from(interactions)
.map(interaction => {
return `${interaction.id}: '${interaction.name}' started at ${interaction.timestamp.toFixed(2)}`;
})
.join("\n");
const interactionMessage = `there were ${interactions.size} interactions:\n${interactionsSummary}`;
}}
>
<div />
</Profiler>;

type ImgProps = React.ComponentProps<'img'>;
// $ExpectType "async" | "auto" | "sync" | undefined
type ImgPropsDecoding = ImgProps['decoding'];
Expand Down