🤖 Merge PR #46643 [@types/react] add VoidFunctionComponent type which does not accept "children" by @awmottaz

Adds a type `VoidFunctionComponent` with `VFC` alias that is the same as
the `FunctionComponent` (`FC`) type, except that it does not accept a
`children` prop.
This commit is contained in:
Tony Mottaz 2020-08-26 14:32:15 -05:00 committed by GitHub
parent e606d5b467
commit 14d95eb0fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -551,6 +551,16 @@ declare namespace React {
displayName?: string;
}
type VFC<P = {}> = VoidFunctionComponent<P>;
interface VoidFunctionComponent<P = {}> {
(props: P, context?: any): ReactElement<any, any> | null;
propTypes?: WeakValidationMap<P>;
contextTypes?: ValidationMap<any>;
defaultProps?: Partial<P>;
displayName?: string;
}
interface ForwardRefRenderFunction<T, P = {}> {
(props: PropsWithChildren<P>, ref: ((instance: T | null) => void) | MutableRefObject<T | null> | null): ReactElement | null;
displayName?: string;

View File

@ -23,6 +23,25 @@ FunctionComponent2.defaultProps = {
};
<FunctionComponent2>24</FunctionComponent2>;
const VoidFunctionComponent: React.VoidFunctionComponent<SCProps> = ({ foo }: SCProps) => {
return <div>{foo}</div>;
};
VoidFunctionComponent.displayName = "VoidFunctionComponent1";
VoidFunctionComponent.defaultProps = {
foo: 42
};
<VoidFunctionComponent />;
// $ExpectError
const VoidFunctionComponent2: React.VoidFunctionComponent<SCProps> = ({ foo, children }) => {
return <div>{foo}{children}</div>;
};
VoidFunctionComponent2.displayName = "VoidFunctionComponent2";
VoidFunctionComponent2.defaultProps = {
foo: 42
};
<VoidFunctionComponent2>24</VoidFunctionComponent2>; // $ExpectError
// svg sanity check
<svg viewBox="0 0 1000 1000">
<g>