1

I am using support for defaultprops in jsx like described in https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#support-for-defaultprops-in-jsx

Everything works fine except in case when I extend props with another interface. In this case I extend my props inside child component like

interface Props extends WithNamespaces { className: string; }

and declare default props:

const defaultProps = { className: '' };

*WithNamespaces is an interface from react-i18next

In my parent component I get compile error that property className is missing for child component even though it should be optional, right?

Is there something that should be done differently to keep these default props not mandatory?

1 Answer 1

1

Props requires className prop. In case it's optional, depending on how the interface is used, it should be marked as such either in interface itself:

interface Props extends WithNamespaces {
  className?: string;
}

Or a place where it's used:

class Comp extends Component<Partial<Props>> {
  static defaultProps: Props = {
    className: ''
  }; 
  ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

But I did initialize className to '' inside defaultProps, as I stated in the question.
Sorry, I missed the comment. Did the answer work for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.