facebook / prop-types

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

TS compiler complains when PropTypes property is set externally from the component.propTypes #406

Closed dominikj111 closed 7 months ago

dominikj111 commented 7 months ago

I have this snippet where the ChippedMultiSelect.propTypes.value passes, but ChippedMultiSelect.propTypes.options doesn't. The const optionItemPropType contains same config as the ChippedMultiSelect.propTypes.value.

The TS compiler complains only about the options config where the issue is

Argument of type 'Requireable<InferProps<{ value: Validator<NonNullable<NonNullable<string | number | null | undefined>>>; label: Validator; }>>' is not assignable to parameter of type 'ValidationMap'.ts(2345)

const optionItemPropType = PropTypes.shape({
    value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
    label: PropTypes.string.isRequired,
});

ChippedMultiSelect.propTypes = {
    value: PropTypes.arrayOf(
        PropTypes.shape({
            value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
            label: PropTypes.string.isRequired,
        }),
    ),
    options: PropTypes.arrayOf(
        PropTypes.oneOfType([
            PropTypes.shape(optionItemPropType),
            PropTypes.shape({
                label: PropTypes.string,
                chips: PropTypes.arrayOf(PropTypes.shape(optionItemPropType)),
                options: PropTypes.arrayOf(PropTypes.shape(optionItemPropType)),
                items: PropTypes.arrayOf(PropTypes.shape(optionItemPropType)),
            }),
        ]),
    ),
};

I would to expect to be able to store the PropTypes config to the variable, is it not that case, or is that an issue?

typescript version: 5.3.3 prop-types version: 15.18.1

ljharb commented 7 months ago

Issues with TS aren't really something this project can fix, and the TS types are kept in the DefinitelyTyped repo, not here.

dominikj111 commented 7 months ago

Trying my luck here then https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/68215