Skip to content

[react]: defaultProps doesn't work when FunctionalComponent<P> is used #30695

@Hotell

Description

@Hotell

Dependencies:

"typescript": "3.2.0-rc"
"@types/react": "16.7.6",
"@types/react-dom": "16.0.9",

As stated by @Kovensky

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/test/managedAttributes.tsx#L167-L175

defaultProps doesn't properly apply on JSX tag when function component is defined via FunctionalComponent

Code:

import React, { FunctionComponent } from 'react'

type Props = {
  onClick: (ev: import('react').MouseEvent<HTMLElement>) => void
  children: import('react').ReactChild
  color: 'red' | 'green'
}

const Button: FunctionComponent<Props> = ({ onClick: handleClick, color, children }) => (
  <button style={{ color }} onClick={handleClick}>
    {children}
  </button>
)
Button.defaultProps = {
  color: 'red'
}

const Test = () => (
  <>
     /* $ExpectError */
    <Button onClick={() => console.log('clicked')}>Click me</Button>
  </>
)

Related:
#29816
microsoft/TypeScript#27425

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy