facebook / prop-types

Runtime type checking for React props and similar objects
MIT License
4.48k stars 356 forks source link

Type error while using prop-types with typescript generics #297

Closed BlueSky1997AL closed 4 years ago

BlueSky1997AL commented 4 years ago

Description

In the following codes:

import PropTypes from 'prop-types';
import React from 'react';

export interface TestProps<T> {
    value?: T;
    onChange?: (param1: string, param2: number, param3: boolean) => void;
}

class Test<T> extends React.Component<TestProps<T>> {
    public static propTypes = {
        onChange: PropTypes.func
    };
}

const el: JSX.Element = <Test onChange={(p1, p2, p3) => console.log(p1, p2, p3)} />;

export default Test;

The onChange attribute of Test component should be in ((param1: string, param2: number, param3: boolean) => void) | undefined type like this:

image

But when I pass a function into this attribute, I get a type error: Parameter 'p1' implicitly has an 'any' type. ts(7006)

image

When I modify value?: T; to value?: T[];, it works as expected:

image

Remove propTypes also works:

image

Remove generics also works:

image

Is this a problem with prop-types?

Environment

ljharb commented 4 years ago

This repo has no types; perhaps you mean to file an issue on DefinitelyTyped?

BlueSky1997AL commented 4 years ago

Opps, I didn't notice that, sorry. I will file an issue on DefinitelyTyped, thanks a lot.