1
我想指定一個函數應該只返回特定類型的React $元素。查看源代碼的下面應該工作,但它並不:
/* @flow */
import React, { Component } from 'react';
type Props = {
testID: string
}
class Price extends Component {
props: Props;
render() {
return null
}
}
function bla() : React$Element<Price> {
return <Price testID="fds"/>;
}
我得到以下錯誤:
18: return <Price testID="fds"/>;
^props of React element `Price`. This type is incompatible with
17: function bla() : React$Element<Price> {
^Price
18: return <Price testID="fds"/>;
^property `testID`. Property not found in
17: function bla() : React$Element<Price> {
^Price
以下是錯誤的發生操場鏈接:
(您可能必須將流版本從v0.43.1切換到v0.43)
編輯:正如Nate在下面提出的,我可以通過Props作爲類型參數,但是如果函數返回具有相同Props類型的另一個React元素,則流程不會投訴。
任何理由downvote? –
我沒有downvote,但我的猜測是,在原始版本的問題中,你只是說它「沒有工作」,並沒有提供任何額外的信息。您已經編輯過包含足夠的信息。 –
另外,我不知道如何解決你的實際問題,但是'React $ Elements'的類型參數是用於道具的,而不是實際的元素類型。如果您將返回類型從'React $元素'更改爲'React $元素',錯誤消失。 –