TS泛型應(yīng)用場景

目前使用monorepo構(gòu)建項(xiàng)目,目前劃分為ui-core,ui-web,ui-native,ui-core用來統(tǒng)一處理邏輯,而ui-web以及ui-native則主要是網(wǎng)頁端以及app端的ui,有些配置,比如

const headerOptions: {
    Icon: FC<IconProps>
    openFn: (v: boolean) => void
    show: boolean
  }[] = [
    {
      Icon: Expand03,
      openFn: () =>
        setMediaData({
          contentFile,
          customSource: CustomMediaSource.GALLERY_PURCHASE_MODAL
        }),
      show: true
    },
    {
      Icon: Edit05,
      openFn: setEditLabel,
      show: true
    }
  ]

這里的IconProps在web端指的是

export interface IconProps extends SVGProps<SVGSVGElement> {
  size?: IconSize
  color?: string
}

而在react-native端則是

import { SvgProps } from "react-native-svg"
import { IconSize } from "./IconSizes"

export interface IconProps extends SvgProps {
  size?: IconSize
  color?: string
}

所以如果我先把這個配置 headerOptions放到ui-core里面,ui-web以及ui-native調(diào)用的時候分別指定泛型才比較合適,最后實(shí)現(xiàn)代碼:

import { FC, useState } from "react"

export type PurchasedContentProps = PurchasedContentCachedProps

interface UsePurchasedContentComponentProps<A>
  extends PurchasedContentCachedProps {
  readonly Expand03: FC<A>
  readonly Edit05: FC<A>
  readonly hoverClass?: string
}

export function usePurchasedContentComponent<A>({
  content,
  Expand03,
  Edit05,
  hoverClass = ""
}: UsePurchasedContentComponentProps<A>) {
  const [editLabel, setEditLabel] = useState(false)

  const { setMediaData } = useMediaModal()
  const contentFile = new ContentFile(content)

  const disableOptions = !content.processed

  let iconClassName = cn(
    !disableOptions
      ? "pointer-events-none text-passes-gray-light/50"
      : "cursor-pointer transition active:opacity-50",
    hoverClass
  )
  if (process.env.IS_REACT_NATIVE) {
    iconClassName = cn(!disableOptions && "text-passes-gray-light/50")
  }

  const headerOptions: {
    Icon: FC<A>
    openFn: (v: boolean) => void
    show: boolean
  }[] = [
    {
      Icon: Expand03,
      openFn: () =>
        setMediaData({
          contentFile,
          customSource: CustomMediaSource.GALLERY_PURCHASE_MODAL
        }),
      show: true
    },
    {
      Icon: Edit05,
      openFn: setEditLabel,
      show: true
    }
  ]

  const { width, height } = content
  const aspectRatio = (width || 1) / (height || 1)

  return {
    aspectRatio,
    iconClassName,
    editLabel,
    setEditLabel,
    headerOptions,
    contentFile,
    setMediaData
  }
}

用的時候指定泛型:

 const {
    contentFile,
    setMediaData,
    aspectRatio,
    iconClassName,
    editLabel,
    setEditLabel,
    headerOptions
  } = usePurchasedContentComponent<IconProps>({
    content,
    Expand03,
    Edit05,
    hoverClass: !isMobile ? "hover:text-passes-gray-light" : ""
  })
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容