Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withTranslation() typing fix for defaultProps #1226

Merged
merged 3 commits into from
Dec 14, 2020

Conversation

kwieccia
Copy link
Contributor

I experienced TS errors when I tried to define props interface for a component that was wrapped in withTranslation() and had defaultProps.

interface IStepperProps extends WithTranslation {  
    isHeadless: boolean; 
    defaultStep: number;  
    isStepInvalid?: boolean;
    submitStep?: () => void;
}

class Stepper extends React.Component<IStepperProps, IStepperState> {
    static defaultProps: Partial<IStepperProps> = {
        isHeadless: true,
        defaultStep: 1,
    };
    
    (...)
} 

export default connect(mapStateToProps)(withTranslation()(Stepper));

The isHeadless and defaultStep props have been marked in the interface as non-optional because they were needed and always provided in defaultProps. But TypeScript complained if I didn't pass them in JSX for the component:

<Stepper>…</Stepper>

TS2739: Type '{ children: any[]; }' is missing the following properties from type 'Readonly<Pick<Pick<IStepperProps, "isHeadless" | "isStepInvalid" | "submitStep" | "defaultStep"> & WithTranslationProps, "i18n" | ... 6 more ... | "useSuspense">>': isHeadless, defaultStep 

After changing the withTranslation() typings in index.d.ts, following this instruction, defaultProps are handled properly.

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • documentation is changed or added (no need)

@coveralls
Copy link

coveralls commented Dec 12, 2020

Coverage Status

Coverage remained the same at 96.372% when pulling 8c61a8f on kwieccia:with-translation-typing-fix into 59758d4 on i18next:master.

@adrai
Copy link
Member

adrai commented Dec 12, 2020

@pedrodurek can you review this?

@pedrodurek
Copy link
Member

Sure!

@pedrodurek
Copy link
Member

Hey @kwieccia, great initiative! 👏
Before I approve it, could you also update the types 4.1, please? They are under ts4.1/index.d.ts file. Thanks!!!

@kwieccia
Copy link
Contributor Author

Sure thing! I pushed the updates.

Copy link
Member

@pedrodurek pedrodurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go!

@jamuhl jamuhl merged commit 182f5c1 into i18next:master Dec 14, 2020
@jamuhl
Copy link
Member

jamuhl commented Dec 14, 2020

was published in react-i18next@11.8.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants