question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Interface OverridableComponent component prop should be optional

See original GitHub issue

In the OverridableComponent.d.ts the interface currently defines the component prop as required. This is incorrect because component is an optional property.

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

A component that extends OverridableComponent should not be required to have a component property. I was able to fix this simply by adding a question mark to the interface definition

export interface OverridableComponent<M extends OverridableTypeMap> {
  <C extends React.ElementType>(props: { component: C } & OverrideProps<M, C>): JSX.Element;
  (props: DefaultComponentProps<M>): JSX.Element;
}

becomes

export interface OverridableComponent<M extends OverridableTypeMap> {
  <C extends React.ElementType>(props: { component?: C } & OverrideProps<M, C>): JSX.Element;
  (props: DefaultComponentProps<M>): JSX.Element;
}

…props: { component?: C }…

Current Behavior 😯

My IDE, IntelliJ Idea, gives a warning on any component that extends OverridableComponent “Element XXX doesn’t have the required attribute component” where XXX is MenuItem, Grid, etc.

Steps to Reproduce 🕹

Create a component that uses any component the extends OverridableComponent without supplying a component prop.

Context 🔦

This is just creating a lot of warnings and I don’t like to ignore warnings.

Your Environment 🌎

IDE IntelliJ Idea 2019.1.3 Ultimate Edition

Tech Version
Material-UI v4.2.1
React v16.8.3
Browser
TypeScript v3.4.5
etc.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rpm911commented, Jul 29, 2019

@rpm911 Have you managed to solve this problem on your side? Were you using the PhpStorm/WebStorm IDE?

I am using IntelliJ Idea Ultimate which includes all the features of PhpStorm and WebStorm. I “solved” the problem by telling the IDE that the component prop is not required. I believe that there are actually two problems, 1) as I stated when I opened the issue, component should be declared as optional, and 2) the JSX compiler is not properly checking that component is required and that is why the builds are succeeding where the IDE gives warnings.

0reactions
ktrzoscommented, Aug 6, 2019

@rpm911

This is a problem with the IDE. Issue ticket has been created here: WEB-40515.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interface OverridableComponent component prop should be ...
d.ts the interface currently defines the component prop as required. This is incorrect because component is an optional property.
Read more >
How to Overload TypeScript React Component Interfaces by ...
This article explains how to create correct, intuitive APIs for React components in TypeScript by using prop values to overload React components.
Read more >
How to pass this TypeScript React component an optional prop?
What you're thinking of is declaring an optional property of a type. For instance: interface Props { bgColor?: string } const a: Props...
Read more >
TypeScript React function Component defined as an ...
export interface OverridableComponent<M extends OverridableTypeMap> { <C extends React. ... The component prop should be optional, and result in no warning.
Read more >
React with TypeScript: Optional Props with Default Values
When using TypeScript with React, it's easy enough to define optional props. With your props interface, you simply apply the optional ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found