如何不使用打字稿重复代码?

syl*_*bre 2 typescript

我正在定义以下功能:

const Text = ({
  align = 'left',
  children,
  className,
  color = 'middleGrey',
  size = 'big',
  light = false,
}: Props) => ()
Run Code Online (Sandbox Code Playgroud)

在使用打字稿时,我定义了以下接口:

interface Props {
  align: string
  children: ReactNode
  className: string
  color: string
  size?: string
  light: boolean
}
Run Code Online (Sandbox Code Playgroud)

但这使我每个属性写两次。

有没有更好的方法来编写这种代码?

Tit*_*mir 5

不幸的是没有。解构语法不支持指定类型注释。这个主题有一个问题,但是它是开放的,不是特别活跃。