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

Can't set children on Modal using TypeScript 3.3.1 #7874

Closed
dlannoye opened this issue Feb 1, 2019 · 12 comments · Fixed by #8189
Closed

Can't set children on Modal using TypeScript 3.3.1 #7874

dlannoye opened this issue Feb 1, 2019 · 12 comments · Fixed by #8189

Comments

@dlannoye
Copy link
Member

dlannoye commented Feb 1, 2019

Environment Information

  • Package version(s): 6.132.0

Please provide a reproduction of the bug in a codepen:

Try to compile the following code using Typescript 3.3.1

import { Modal} from 'office-ui-fabric-react/lib/Modal';
import * as React from 'react';

export const MyModal: React.SFC = () => {
    return (
        <Modal>
            <div>hello world</div>
        </Modal>
    );
}

Actual behavior:

Compile error

index.tsx:6:10 - error TS2559: Type '{ children: Element; }' has no properties in common with type 'IntrinsicAttributes & IModalProps'.

6         <Modal>
           ~~~~~


Found 1 error.

Expected behavior:

No compile error

Priorities and help requested:

Are you willing to submit a PR to fix? No

Requested priority: High

Products/sites affected: (if applicable)

@asmundg
Copy link
Member

asmundg commented Feb 5, 2019

Looks like TS 3.3's TSX checks that it can pass children as part of the component props (the output js passes everything in createElement's props argument).

This probably means that functional components need to include children in their props interface (IModalProps in this case).

The FunctionComponent in newer @types/react versions explicitly adds children to the accepted props:

    interface FunctionComponent<P = {}> {
        (props: P & { children?: ReactNode }, context?: any): ReactElement<any> | null;
        propTypes?: ValidationMap<P>;
        contextTypes?: ValidationMap<any>;
        defaultProps?: Partial<P>;
        displayName?: string;
    }

@dlannoye
Copy link
Member Author

dlannoye commented Feb 5, 2019

For a workaround I was able to do the following type augmentation:

declare module 'office-ui-fabric-react/lib/Modal' {
 const Modal: React.StatelessComponent<IModalProps>;
}

I noticed that the Dialog component didn't have this issue and saw that it was using React.StatelessComponent as the type rather then relying on the implicit type generated by the styled HOC.

Putting this explicit type would fix the issue, but this makes me wonder if the return type of the styled HOC should be looked at for a possible root cause.

@bagau
Copy link

bagau commented Feb 11, 2019

I spent many time to understand where I should set this declare module.

declare module 'office-ui-fabric-react/lib/Modal' {
 const Modal: React.StatelessComponent<IModalProps>;
}

May be some people doesn't understand too.
This code I pasted to react-app-env.d.ts file and now it's working.

@jdhuntington jdhuntington self-assigned this Mar 5, 2019
jdhuntington pushed a commit to jdhuntington/office-ui-fabric-react that referenced this issue Mar 5, 2019
Replace previous return type of (props) => JSX.Element
Also added type annotations to all usages of styled due to TypeScript compiler.
dzearing pushed a commit that referenced this issue Mar 5, 2019
* Modify styled to return StatelessComponent (#7874)

Replace previous return type of (props) => JSX.Element
Also added type annotations to all usages of styled due to TypeScript compiler.

* Changelog
@msft-github-bot
Copy link
Contributor

🎉This issue was addressed in #8189, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

@msft-github-bot
Copy link
Contributor

🎉This issue was addressed in #8189, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

@msft-github-bot
Copy link
Contributor

🎉This issue was addressed in #8189, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

@msft-github-bot
Copy link
Contributor

🎉This issue was addressed in #8189, which has now been successfully released as [email protected].:tada:

Handy links:

@msft-github-bot
Copy link
Contributor

🎉This issue was addressed in #8189, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

@msft-github-bot
Copy link
Contributor

🎉This issue was addressed in #8189, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

aftab-hassan pushed a commit to aftab-hassan/office-ui-fabric-react that referenced this issue Mar 12, 2019
* Modify styled to return StatelessComponent (microsoft#7874)

Replace previous return type of (props) => JSX.Element
Also added type annotations to all usages of styled due to TypeScript compiler.

* Changelog
@Paul-Colucci
Copy link

I am also experiencing this issue. How do I resolve this?

@Paul-Colucci
Copy link

Updating to the latest version of office-ui-fabric-react (v6.166.0) worked for me

@juanpvy
Copy link

juanpvy commented May 22, 2019

Yes, version office-ui-fabric-react (v6.166.0) works fine

@microsoft microsoft locked as resolved and limited conversation to collaborators Aug 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants