简体   繁体   English

描述到打字稿反应组件包装器

[英]Describe to typescript react component wrapper

How to describe wrapper around any children that tippy accepts that typescript will use correctly?如何描述tippy接受打字稿将正确使用的任何孩子的包装器?

import Tippy, { TippyProps } from '@tippy.js/react'
import React from 'react'
import 'tippy.js/dist/tippy.css'

Tippy.defaultProps = {
  maxWidth: '500px',
}

export const Tooltip: Tooltip = ({ children, content, ...rest }) => {
  if (!children) return null
  if (!content) return children

  return (
    <Tippy content={content} {...rest}>
      {children}
    </Tippy>
  )
}

interface Tooltip {
  (props: MyProps): React.ForwardRefExoticComponent<TippyProps> | null | React.ReactNode
}

interface MyProps {
  content?: string | React.ReactNode
  children?: React.ReactNode
}

Used like this:像这样使用:

   <Tooltip content='Text'>
        <div>123</div>
   </Tooltip>

Typescript gives me error for children in return statement:打字稿在 return 语句中为孩子们提供了错误:

Type 'string |输入'字符串| number |数量 | true |真实| {} | {} | ReactElement ReactElement Component)> | ReactElement ReactElement 组件)> | null) |空) | (new (props: any) => Component)> | (new (props: any) => 组件)> | ReactNodeArray |反应节点阵列 | ReactPortal' is not assignable to type 'ReactElement ReactElement Component)> | ReactPortal' 不可分配到类型 'ReactElement ReactElement 组件)> | null) |空) | (new (props: any) => Component)>'. (new (props: any) => 组件)>'. Type 'string' is not assignable to type 'ReactElement ReactElement Component)> |类型 'string' 不可分配给类型 'ReactElement ReactElement Component)> | null) |空) | (new (props: any) => Component)>'.ts(2322) index.d.ts(6, 3): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & TippyProps' (new (props: any) => Component)>'.ts(2322) index.d.ts(6, 3): 预期的类型来自属性 'children',它在类型 'IntrinsicAttributes & TippyProps' 上声明

The error points you right at the problem: you've declared children to be a ReactNode but ReactNode can be a string and Tippy (apparently) won't accept string children.该错误指出了您的问题:您已将 children 声明为 ReactNode,但 ReactNode 可以是字符串,并且 Tippy(显然)不会接受字符串 children。 Use ReactElement instead.改用 ReactElement。 When debugging typescript errors like this it is frequently helpful (at least until you get more used to reading them) to add whitespace formatting:在调试这样的打字稿错误时,添加空格格式通常很有帮助(至少在您更习惯阅读它们之前):

Type 
'string | number | true | {} | ReactElement ReactElement Component)> | null) 
| (new (props: any) => Component)> 
| ReactNodeArray 
| ReactPortal' 
is not assignable to type 
'ReactElement ReactElement Component)> | null) 
| (new (props: any) => Component)>'

Type 'string' is not assignable to type 
'ReactElement ReactElement Component)> | null) 
| (new (props: any) => Component)>'

ts(2322) index.d.ts(6, 3): The expected type comes from property 'children' 
which is declared here on type 'IntrinsicAttributes & TippyProps'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM