2017-09-15 69 views
0

比方說,我有一個組件Card,它看起來像這樣丟失後title屬性類型檢查包裝陣營組件與injectIntl​​()

<Card title="Example" /> 

我需要從react-intl包裹與injectIntl()此組件,所以intl上下文被注入。我做這樣的:

import * as React from 'react'; 
import {injectIntl} from 'react-intl'; 

interface Props { 
    title: string; 
} 

class Component extends React.Component<Props, null> { 
    // .. 
} 

export const Card = injectIntl(Component); 

現在我可以用Card組件,而不屬性title

<Card /> 

這似乎對打字稿編譯器來說很好。也沒有運行時錯誤。我預計,編譯器會引發這樣的錯誤:

Property 'title' is missing in type '{}'. 

來自哪裏這種行爲?這是爲什麼?如何解決這個問題呢?

預先感謝您!

回答

1

TL; DR:您需要安裝@types/react-intl包。

react-intl軟件包不使用類型註釋,正如您在inject.js的源中所見。由injectIntl返回的組件與您的Card完全不同。 @types/react-intl包中的類型添加缺少的類型註釋。

+0

謝謝@Tomáš!你的回答指出我正確的方向。其實我沒有安裝這些類型,所以在編譯時打字稿不知道要驗證什麼。也許你可以用這個提示更新你的問題? https://www.npmjs.com/package/@types/react-intl – Robin

+0

@Robin答案已更新。接得好!我忘記了Typescript有一個類型註釋的外部存儲庫。 –