Skip to content

Commit 4e6b33d

Browse files
committed
refactor: migrate ReplaceTransition to TypeScript
1 parent 8673957 commit 4e6b33d

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/ReplaceTransition.js renamed to src/ReplaceTransition.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
3+
import type { ReactElement } from 'react';
34
import ReactDOM from 'react-dom';
45
import TransitionGroup from './TransitionGroup';
6+
import type { Props as TransitionProps } from './Transition';
7+
8+
type Props = Omit<TransitionProps, 'children'> & {
9+
children: [ReactElement<TransitionProps>, ReactElement<TransitionProps>];
10+
};
511

612
/**
713
* The `<ReplaceTransition>` component is a specialized `Transition` component
@@ -14,32 +20,35 @@ import TransitionGroup from './TransitionGroup';
1420
* </ReplaceTransition>
1521
* ```
1622
*/
17-
class ReplaceTransition extends React.Component {
18-
handleEnter = (...args) => this.handleLifecycle('onEnter', 0, args);
19-
handleEntering = (...args) => this.handleLifecycle('onEntering', 0, args);
20-
handleEntered = (...args) => this.handleLifecycle('onEntered', 0, args);
23+
class ReplaceTransition extends React.Component<Props> {
24+
handleEnter = (...args: any) => this.handleLifecycle('onEnter', 0, args);
25+
handleEntering = (...args: any) =>
26+
this.handleLifecycle('onEntering', 0, args);
27+
handleEntered = (...args: any) => this.handleLifecycle('onEntered', 0, args);
2128

22-
handleExit = (...args) => this.handleLifecycle('onExit', 1, args);
23-
handleExiting = (...args) => this.handleLifecycle('onExiting', 1, args);
24-
handleExited = (...args) => this.handleLifecycle('onExited', 1, args);
29+
handleExit = (...args: any) => this.handleLifecycle('onExit', 1, args);
30+
handleExiting = (...args: any) => this.handleLifecycle('onExiting', 1, args);
31+
handleExited = (...args: any) => this.handleLifecycle('onExited', 1, args);
2532

26-
handleLifecycle(handler, idx, originalArgs) {
33+
handleLifecycle(handler: any, idx: number, originalArgs: any) {
2734
const { children } = this.props;
28-
const child = React.Children.toArray(children)[idx];
35+
// @ts-expect-error FIXME: Type 'string' is not assignable to type 'ReactElement<Props, string | JSXElementConstructor<any>>'.ts(2322)
36+
const child: ChildElement = React.Children.toArray(children)[idx];
2937

3038
if (child.props[handler]) child.props[handler](...originalArgs);
39+
// @ts-expect-error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>'.ts(7053)
3140
if (this.props[handler]) {
3241
const maybeNode = child.props.nodeRef
3342
? undefined
3443
: ReactDOM.findDOMNode(this);
35-
44+
// @ts-expect-error FIXME: Argument of type 'Element | Text | null | undefined' is not assignable to parameter of type 'HTMLElement'.ts(2769)
3645
this.props[handler](maybeNode);
3746
}
3847
}
3948

4049
render() {
41-
const { children, in: inProp, ...props } = this.props;
42-
const [first, second] = React.Children.toArray(children);
50+
const { children, in: inProp, ...props }: any = this.props;
51+
const [first, second]: any = React.Children.toArray(children);
4352

4453
delete props.onEnter;
4554
delete props.onEntering;
@@ -68,9 +77,10 @@ class ReplaceTransition extends React.Component {
6877
}
6978
}
7079

80+
// @ts-expect-error To make TS migration diffs minimum, I've left propTypes here instead of defining a static property
7181
ReplaceTransition.propTypes = {
7282
in: PropTypes.bool.isRequired,
73-
children(props, propName) {
83+
children(props: any, propName: any) {
7484
if (React.Children.count(props[propName]) !== 2)
7585
return new Error(
7686
`"${propName}" must be exactly two transition components.`

0 commit comments

Comments
 (0)