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.

[TS]: Fix Component Props declarations

See original GitHub issue

Pay attention to rules for migration at the end of the issue.

Expected Behavior

Be able to import and extend the <component>Props correctly, bringing the JSX.IntrinsicElements props together.

Actual Behavior

Components have theirs on <component>Props combined with JSX.IntrinsicElements being exported using the declare block. The import of <component>Props doesn’t have all the props defined in the declare block.

References: https://github.com/grommet/grommet/pull/4909 https://github.com/grommet/grommet/issues/4976

Proposed Solution

As discussed in the PR: https://github.com/grommet/grommet/pull/4909, We need to check which components follow the pattern of combining the props on declare blocks. A second item is that many components are defined as React.ComponentClass but can be modified to React.FC.

That being said, we should start refining these declarations following the examples below, where we create and export an extended version of the props instead of removing the actual export:

export interface LayerProps {...}
export type LayerPropsExtended = LayerProps & JSX.IntrinsicElements['div'] 

// React.FC or React.componentClass must be evaluated to see if the change is possible.
// Preference to use React.FC
declare const Layer: React.FC<LayerPropsExtended>;

export { Layer };
// another example
export interface TextInputProps { ... };

export interface TextInputExtendedProps extends Omit<
JSX.IntrinsicElements['input'],
'onSelect' | 'size' | 'placeholder'
>, TextInputProps {};

declare const TextInput: React.FC<TextInputExtendedProps>;

export { TextInput };

Roadmap

Thanks to @Isaius for check and get this list for us.

These do not extend JSX.IntrinscElements in their props interface but in the declare block.

These are OK, but do not follow the pattern, using Type instead interface:

Only ComponentClass, but not wrong:

Rules for Migration

  • Pick a maximum of 4 components at the same time
  • Comment which one you will work
  • Work is done! Create the PR with a maximum of 4 components per PR
  • Wait for the review and the merge. After merged, you can pick new ones.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

5reactions
ericsoderberghpcommented, Feb 20, 2021

I believe that we need to revisit the approach taken in #4909 . If there are existing callers who have extended the current ComponentProps to add some instrinsic properties, I think their code will break due to the new properties already existing. I’m thinking we should preserve the existing behavior and add a new export for the extension:

export interface TextInputProps { ... };

export interface TextInputExtendedProps extends Omit<
JSX.IntrinsicElements['input'],
'onSelect' | 'size' | 'placeholder'
>, TextInputProps {};

declare const TextInput: React.FC<TextInputExtendedProps>;

export { TextInput };
1reaction
abnersajrcommented, Feb 18, 2021

Thank you @Isaius for the contribution. The list was added to the issue. Just to not bloat the discussion, could you delete it please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining Props in React Function Component with Typescript
This guide will provide you with the syntax you need to properly define the props entering your components, compare and contrast defining ...
Read more >
TypeScript Error in React component on useState declaration ...
The first error is on passing function via props to a child component, and the second one is on declaring useState using a...
Read more >
Documentation - JSX - TypeScript
As the name suggests, the component is defined as a JavaScript function where its first argument is a props object. TS enforces that...
Read more >
Typing Component Props - React TypeScript Cheatsheets
Typing Component Props. This is intended as a basic orientation and reference for React developers familiarizing with TypeScript.
Read more >
React with TypeScript: Components as Function Declarations ...
Typing a React Component as a Function Expression · Typing a React Component as a Function Declaration · Typing Props with a Function...
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