[react-redux] Add NonReactStatics to ConnectedComponentClass (#33446)

This commit is contained in:
Nathan Shively-Sanders
2019-03-06 15:34:12 -08:00
committed by GitHub
2 changed files with 48 additions and 1 deletions
+6 -1
View File
@@ -44,6 +44,8 @@ import {
Store
} from 'redux';
import { NonReactStatics } from 'hoist-non-react-statics';
// Omit taken from https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
@@ -96,7 +98,10 @@ export type GetProps<C> = C extends ComponentType<infer P> ? P : never;
// Applies LibraryManagedAttributes (proper handling of defaultProps
// and propTypes), as well as defines WrappedComponent.
export type ConnectedComponentClass<C, P> = ComponentClass<JSX.LibraryManagedAttributes<C, P>> & {
export type ConnectedComponentClass<
C extends ComponentType<any>,
P
> = ComponentClass<JSX.LibraryManagedAttributes<C, P>> & NonReactStatics<C> & {
WrappedComponent: C;
};
+42
View File
@@ -1167,6 +1167,48 @@ function TestLibraryManagedAttributes() {
<ConnectedComponent2 fn={() => { }} />;
}
function TestNonReactStatics() {
interface OwnProps {
bar: number;
}
interface MapStateProps {
foo: string;
}
class Component extends React.Component<OwnProps & MapStateProps> {
static defaultProps = {
bar: 0,
};
static meaningOfLife = 42;
render() {
return <div />;
}
}
function mapStateToProps(state: any): MapStateProps {
return {
foo: 'foo',
};
}
Component.meaningOfLife;
Component.defaultProps.bar;
const ConnectedComponent = connect(mapStateToProps)(Component);
// This is a non-React static and should be hoisted as-is.
ConnectedComponent.meaningOfLife;
// This is a React static, so it's not hoisted.
// However, ConnectedComponent is still a ComponentClass, which specifies `defaultProps`
// as an optional static member. We can force an error (and assert that `defaultProps`
// wasn't hoisted) by reaching into the `defaultProps` object without a null check.
ConnectedComponent.defaultProps.bar; // $ExpectError
}
function TestProviderContext() {
const store: Store = createStore((state = {}) => state);
const nullContext = React.createContext(null);