Skip to content

Default type in generic react component not inherited in callbacksΒ #50858

Closed
@mc-petry

Description

@mc-petry

Bug Report

πŸ”Ž Search Terms

React component default generic type

πŸ•— Version & Regression Information

  • This is an error

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

import React from 'react'
import {
  ComponentPropsWithRef,
  ElementType,
  ReactNode,
} from 'react'

type ButtonBaseProps<T extends ElementType> =
   ComponentPropsWithRef<T> & {
  children?: ReactNode
}

function Component<T extends ElementType = 'span'>(props: ButtonBaseProps<T>) {
  return <></>
}

const v1 = <Component onClick={e => e.preventDefault()} /> // <- Error
const v2 = <Component<'span'> onClick={e => e.preventDefault()} /> // <- Ok

πŸ™ Actual behavior

Default generic type is not inherited in callbacks, but intellisense works fine:

image

πŸ™‚ Expected behavior

Should work both variants

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueHelp WantedYou can do this

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions