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 '{}'.
來自哪裏這種行爲?這是爲什麼?如何解決這個問題呢?
預先感謝您!
謝謝@Tomáš!你的回答指出我正確的方向。其實我沒有安裝這些類型,所以在編譯時打字稿不知道要驗證什麼。也許你可以用這個提示更新你的問題? https://www.npmjs.com/package/@types/react-intl – Robin
@Robin答案已更新。接得好!我忘記了Typescript有一個類型註釋的外部存儲庫。 –