I'm working with TypeScript on React, plus I'm validating props on runtime with PropTypes. Recently I updated to TypeScript 3.9.3 and since then things are breaking. Given this interface:
interface StoryQuestionsProps extends WithMediaProps {
questionObject: {
question: string,
answer: string,
nid?: string,
path?: string,
type?: string,
},
children?: JSX.Element | string,
}
If I declare these PropTypes:
StoryQuestions.propTypes = {
questionObject: PropTypes.shape({
question: PropTypes.string.isRequired,
answer: PropTypes.string.isRequired,
nid: PropTypes.string,
path: PropTypes.string,
type: PropTypes.string,
}).isRequired,
children: PropTypes.oneOfType([
PropTypes.element,
PropTypes.string,
]),
}
Throws error TS2322:
Type 'Validator; answer: Validator; nid: Requireable; path: Requireable; type: Requireable; }>>' is not assignable to type 'Validator<{ question: string; answer: string; nid?: string | undefined; path?: string | undefined; type?: string | undefined; }>'. Type 'InferProps<{ question: Validator; answer: Validator; nid: Requireable; path: Requireable; type: Requireable; }>' is not assignable to type '{ question: string; answer: string; nid?: string | undefined; path?: string | undefined; type?: string | undefined; }'. Types of property 'nid' are incompatible. Type 'string | null | undefined' is not assignable to type 'string | undefined'. Type 'null' is not assignable to type 'string | undefined'.
Instead, expecting any type:
StoryQuestions.propTypes = {
questionObject: PropTypes.any.isRequired,
children: PropTypes.oneOfType([
PropTypes.element,
PropTypes.string,
]),
}
it doesn't return errors, but this way I would miss the questionObject
properties validation. Why propType.shape()
is not working?
I've also tried wrapping propTypes.shape()
with propTypes.objectOf(PropTypes.shape( ... ))
but has no effect.