简体   繁体   English

如何通过将未定义的prop遗漏在react / typescript项目的声明中来传递它?

[英]How to pass prop as undefined by leaving it out in a declaration in react/typescript project?

I wan't to do this 我想做这个

标签缺失错误

But i get an typescript error saying that label is missing. 但是我收到一个打字稿错误,说标签丢失了。 So when i explicitly say that it should be undefined the error disappears like in the image below 因此,当我明确地说应该未定义时,错误消失,如下图所示

没有错误

My interface looks like this 我的界面看起来像这样

export default interface Props {
  onPress: () => void
  label: string | null
}

I want to be able to leave it out and thereby passing undefined. 我希望能够省略它,从而传递不确定的信息。 Do some Compiler options exist for this? 为此是否存在一些编译器选项? Or a workaround in the interface? 还是界面中的解决方法? Or some other way around this? 还是其他解决方法?

You can make the field optional using ? 您可以使用? :

export default interface Props {
  onPress: () => void
  label?: string
}

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

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