Flow:使用通用类型的可选道具
Flow: optional prop that uses generic types
我有一个 React 组件,它有一个名为 showModal
的函数属性。我在其他地方使用泛型类型注释了这个函数,但在这里,我希望它是一个可选的 prop。通常我用 func?: () => void
来做到这一点,但这种语法在这种情况下不起作用(无论我把 ?
放在哪里,我都会得到 Parsing error
):
type props = {
showModal<T>(T => React$Node, T): void,
}
如何指定 showModal
是可选的?我查看了 Flow 的文档,但找不到与此问题相关的任何内容。
您需要调整函数表达式。 Flow Try link
type Props = {
showModal?: <T>(T => React$Node, T) => void,
}
const foo: Props = {
showModal: (a, b) => undefined
}
const bar: Props = {
showModal: undefined
}
我有一个 React 组件,它有一个名为 showModal
的函数属性。我在其他地方使用泛型类型注释了这个函数,但在这里,我希望它是一个可选的 prop。通常我用 func?: () => void
来做到这一点,但这种语法在这种情况下不起作用(无论我把 ?
放在哪里,我都会得到 Parsing error
):
type props = {
showModal<T>(T => React$Node, T): void,
}
如何指定 showModal
是可选的?我查看了 Flow 的文档,但找不到与此问题相关的任何内容。
您需要调整函数表达式。 Flow Try link
type Props = {
showModal?: <T>(T => React$Node, T) => void,
}
const foo: Props = {
showModal: (a, b) => undefined
}
const bar: Props = {
showModal: undefined
}