mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-01-12 22:46:38 +08:00
[recompose] improve type inference of static property helpers
This commit is contained in:
17
types/recompose/index.d.ts
vendored
17
types/recompose/index.d.ts
vendored
@@ -1,10 +1,11 @@
|
||||
// Type definitions for Recompose 0.26
|
||||
// Type definitions for Recompose 0.27
|
||||
// Project: https://github.com/acdlite/recompose
|
||||
// Definitions by: Iskander Sierra <https://github.com/iskandersierra>
|
||||
// Samuel DeSota <https://github.com/mrapogee>
|
||||
// Curtis Layne <https://github.com/clayne11>
|
||||
// Rasmus Eneman <https://github.com/Pajn>
|
||||
// Lucas Terra <https://github.com/lucasterra>
|
||||
// Brian Adams <https://github.com/brian-lives-outdoors>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.8
|
||||
|
||||
@@ -269,19 +270,19 @@ declare module 'recompose' {
|
||||
// Static property helpers: https://github.com/acdlite/recompose/blob/master/docs/API.md#static-property-helpers
|
||||
|
||||
// setStatic: https://github.com/acdlite/recompose/blob/master/docs/API.md#setStatic
|
||||
export function setStatic<TOutter>(
|
||||
export function setStatic(
|
||||
key: string, value: any
|
||||
): ComponentEnhancer<TOutter, TOutter>;
|
||||
): <T extends Component>(component: T) => T;
|
||||
|
||||
// setPropTypes: https://github.com/acdlite/recompose/blob/master/docs/API.md#setPropTypes
|
||||
export function setPropTypes<TOutter>(
|
||||
propTypes: ValidationMap<TOutter>
|
||||
): ComponentEnhancer<any, TOutter>;
|
||||
export function setPropTypes<P>(
|
||||
propTypes: ValidationMap<P>
|
||||
): <T extends Component<P>>(component: T) => T;
|
||||
|
||||
// setDisplayName: https://github.com/acdlite/recompose/blob/master/docs/API.md#setDisplayName
|
||||
export function setDisplayName<TOutter>(
|
||||
export function setDisplayName(
|
||||
displayName: string
|
||||
): ComponentEnhancer<TOutter, TOutter>;
|
||||
): <T extends Component>(component: T) => T;
|
||||
|
||||
|
||||
// Utilities: https://github.com/acdlite/recompose/blob/master/docs/API.md#utilities
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as React from "react";
|
||||
import * as PropTypes from 'prop-types';
|
||||
import {
|
||||
// Higher-order components
|
||||
mapProps, withProps, withPropsOnChange, withHandlers,
|
||||
@@ -412,3 +413,86 @@ function testLifecycle() {
|
||||
}
|
||||
})(component)
|
||||
}
|
||||
|
||||
function testSetStatic() {
|
||||
interface Props {
|
||||
foo: string;
|
||||
}
|
||||
|
||||
let SfcResult: React.SFC<Props>;
|
||||
const SfcComp: React.SFC<Props> = (props) => (<div>{props.foo}</div>);
|
||||
|
||||
let ClassResult: React.ComponentClass<Props, {}>;
|
||||
class ClassComp extends React.Component<Props> {
|
||||
render() {
|
||||
return (<div>{this.props.foo}</div>);
|
||||
}
|
||||
}
|
||||
|
||||
const hoc1 = setStatic('bar', 'a string');
|
||||
const hoc2 = setStatic('bar', 5);
|
||||
const hoc3 = setStatic('bar', { a: 'b' });
|
||||
|
||||
SfcResult = hoc1(SfcComp);
|
||||
SfcResult = hoc2(SfcComp);
|
||||
SfcResult = hoc3(SfcComp);
|
||||
SfcResult = hoc1(ClassComp); // $ExpectError
|
||||
|
||||
ClassResult = hoc1(ClassComp);
|
||||
ClassResult = hoc2(ClassComp);
|
||||
ClassResult = hoc3(ClassComp);
|
||||
ClassResult = hoc1(SfcComp); // $ExpectError
|
||||
}
|
||||
|
||||
function testSetPropTypes() {
|
||||
interface Props {
|
||||
foo: string;
|
||||
}
|
||||
const validationMap = {
|
||||
foo: PropTypes.string.isRequired
|
||||
}
|
||||
|
||||
let SfcResult: React.SFC<Props>;
|
||||
const SfcComp: React.SFC<Props> = (props) => (<div>{props.foo}</div>);
|
||||
|
||||
let ClassResult: React.ComponentClass<Props, {}>;
|
||||
class ClassComp extends React.Component<Props> {
|
||||
render() {
|
||||
return (<div>{this.props.foo}</div>);
|
||||
}
|
||||
}
|
||||
|
||||
const hoc = setPropTypes(validationMap);
|
||||
|
||||
SfcResult = hoc(SfcComp);
|
||||
SfcResult = hoc(ClassComp); // $ExpectError
|
||||
|
||||
ClassResult = hoc(ClassComp);
|
||||
ClassResult = hoc(SfcComp); // $ExpectError
|
||||
|
||||
SfcResult = setPropTypes({ bar: PropTypes.string })(SfcComp); // $ExpectError
|
||||
}
|
||||
|
||||
function testSetDisplayName() {
|
||||
interface Props {
|
||||
foo: string;
|
||||
}
|
||||
|
||||
let SfcResult: React.SFC<Props>;
|
||||
const SfcComp: React.SFC<Props> = (props) => (<div>{props.foo}</div>);
|
||||
|
||||
let ClassResult: React.ComponentClass<Props, {}>;
|
||||
class ClassComp extends React.Component<Props> {
|
||||
render() {
|
||||
return (<div>{this.props.foo}</div>);
|
||||
}
|
||||
}
|
||||
|
||||
const hoc = setDisplayName('NewDisplayName');
|
||||
|
||||
SfcResult = hoc(SfcComp);
|
||||
SfcResult = hoc(ClassComp); // $ExpectError
|
||||
|
||||
ClassResult = hoc(ClassComp);
|
||||
ClassResult = hoc(SfcComp); // $ExpectError
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user